Skip to content

Commit c523a90

Browse files
committed
aws-deploy
1 parent 20ccc2e commit c523a90

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

.github/workflows/deploy.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Deploy to AWS Lambda
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
env:
12+
AWS_REGION: ap-south-1
13+
AWS_ACCOUNT_ID: 200077350985
14+
REPO_NAME: ootsav-lambda-backend
15+
IMAGE_TAG: latest
16+
FUNCTION_NAME: ootsav-backend
17+
ROLE_ARN: arn:aws:iam::200077350985:role/lambda-basic-execution-role
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Node.js
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: 18
27+
28+
- name: Install dependencies
29+
run: npm ci
30+
31+
- name: Build TypeScript
32+
run: npm run build
33+
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3
36+
37+
- name: Configure AWS credentials
38+
uses: aws-actions/configure-aws-credentials@v4
39+
with:
40+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
41+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
42+
aws-region: ${{ env.AWS_REGION }}
43+
44+
- name: Login to Amazon ECR
45+
run: |
46+
aws ecr get-login-password --region $AWS_REGION \
47+
| docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
48+
49+
- name: reate ECR repo if needed
50+
run: |
51+
aws ecr describe-repositories --repository-names $REPO_NAME \
52+
|| aws ecr create-repository --repository-name $REPO_NAME
53+
54+
- name: Build Docker image
55+
run: |
56+
docker build -t $REPO_NAME .
57+
docker tag $REPO_NAME:$IMAGE_TAG ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/$REPO_NAME:$IMAGE_TAG
58+
59+
- name: Push image to ECR
60+
run: |
61+
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/$REPO_NAME:$IMAGE_TAG
62+
63+
- name: Deploy Lambda function
64+
run: |
65+
set -e
66+
if aws lambda get-function --function-name $FUNCTION_NAME; then
67+
echo "Updating function code..."
68+
aws lambda update-function-code \
69+
--function-name $FUNCTION_NAME \
70+
--image-uri ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/$REPO_NAME:$IMAGE_TAG
71+
else
72+
echo "Creating new Lambda function..."
73+
aws lambda create-function \
74+
--function-name $FUNCTION_NAME \
75+
--package-type Image \
76+
--code ImageUri=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/$REPO_NAME:$IMAGE_TAG \
77+
--role $ROLE_ARN
78+
fi
79+
80+
- name: Ensure Lambda URL exists
81+
run: |
82+
aws lambda get-function-url-config --function-name $FUNCTION_NAME \
83+
|| aws lambda create-function-url-config \
84+
--function-name $FUNCTION_NAME \
85+
--auth-type NONE
86+
87+
- name: Ensure public permission
88+
run: |
89+
aws lambda add-permission \
90+
--function-name $FUNCTION_NAME \
91+
--action lambda:InvokeFunctionUrl \
92+
--principal "*" \
93+
--function-url-auth-type NONE \
94+
--statement-id function-url-public-access \
95+
|| echo "Permission already set"

0 commit comments

Comments
 (0)