Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gakas14/deploy-python-docker-image-on-aws-lambda
https://github.com/gakas14/deploy-python-docker-image-on-aws-lambda
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/gakas14/deploy-python-docker-image-on-aws-lambda
- Owner: gakas14
- Created: 2024-01-30T12:41:25.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-01T06:19:44.000Z (10 months ago)
- Last Synced: 2024-02-01T07:27:50.081Z (10 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Deploy-Python-Docker-Image-on-AWS-Lambda
This project aims to get the current Bitcoin price using an API call. We build this project using AWS CDK, Docker, and AWS lambda.### Create AWS CDK project
```
cdk init app --language typescript
```### Create a Python hander and a docker file
- create a folder image and sub-folder src
- create a Python file inside /src.
- create a docker file and a requirements file inside the image.### configure the files
##### Dockerfile
```
# Base image
FROM public.ecr.aws/lambda/python:3.11# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}# Install the specified packages
RUN pip install -r requirements.txt# Copy all files in ./src
COPY src/* ${LAMBDA_TASK_ROOT}#Set the CMD to your handler
CMD ["main.handler"]
```
#### requirements.tx
```
requests
```#### docker-lambda_stack.ts
```
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as lambda from "aws-cdk-lib/aws-lambda";export class DockerBitcoinStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);const dockerFunc = new lambda.DockerImageFunction(this, "DockerFunc", {
code:lambda.DockerImageCode.fromImageAsset("./image"),
memorySize: 1024,
timeout: cdk.Duration.seconds(10),});
const functionUrl = dockerFunc.addFunctionUrl({
authType:lambda.FunctionUrlAuthType.NONE,
cors: {
allowedMethods: [lambda.HttpMethod.ALL],
allowedHeaders: ["*"],
allowedOrigins: ["*"],
},});
new cdk.CfnOutput(this, "FunctionUrlValue", {
value: functionUrl.url,});
}
}
```
#### Test the docker image locally
```
docker build -t docker-image:test .
docker run -p 9000:8080 docker-image:test
```### Deploy the project on AWS
```
cdk bootstrap -region <>
cdk deploy
```