Skip to content

Commit fa08011

Browse files
author
Adetokunbo Ige
committed
feat: code improvements
Signed-off-by: Adetokunbo Ige <[email protected]>
1 parent 5031ef2 commit fa08011

File tree

6 files changed

+196
-95
lines changed

6 files changed

+196
-95
lines changed

.github/workflows/docker-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ jobs:
8080
id: build-push-to-ecr
8181
with:
8282
context: todo-app/lambda_function
83-
file: Dockerfile
83+
file: todo-app/lambda_function/Dockerfile
8484
push: true
8585
tags: ${{ env.ECR_REGISTRY }}/${{ env.REPO_NAME }}:${{ steps.sha_short.outputs.sha_short }}
86-
platforms: linux/amd64,linux/arm64
86+
platforms: linux/amd64
8787
provenance: false
8888
continue-on-error: false

.github/workflows/pulumi-deploy.yml

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,38 @@ jobs:
1313

1414
steps:
1515
- name: Checkout code
16-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1717

18-
- name: Set up Pulumi
19-
uses: pulumi/actions@v3
18+
- name: Configure AWS credentials
19+
uses: aws-actions/configure-aws-credentials@v4 # More information on this action can be found below in the 'AWS Credentials' section
2020
with:
21-
command: up
22-
env:
23-
# Set up Pulumi credentials
24-
PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}
25-
26-
- name: Configure AWS credentials # Only required if deploying to AWS
27-
uses: aws-actions/configure-aws-credentials@v2
28-
with:
29-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
30-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31-
aws-region: your_aws_region
21+
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
22+
aws-region: ${{ secrets.AWS_REGION }}
23+
role-session-name: GithubActionsSession
3224

3325
- name: Install Dependencies
26+
working-directory: todo-app
3427
run: |
28+
pip install --upgrade pip
3529
pip install -r requirements.txt
3630
37-
- name: Run Pulumi Deployment
31+
- name: Configure Pulumi
32+
working-directory: todo-app
33+
run: |
34+
pulumi stack select igeadetokunbo/todo-app/dev --non-interactive || pulumi stack init igeadetokunbo/todo-app/dev
35+
env:
36+
PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}
37+
38+
- name: Pulumi Preview
39+
working-directory: todo-app
40+
run: |
41+
pulumi preview --stack ExitoLab/todo-app/dev --yes --non-interactive
42+
env:
43+
PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}
44+
45+
- name: Pulumi Up
46+
working-directory: todo-app
3847
run: |
39-
pulumi stack select your-pulumi-stack
40-
pulumi config set imageName ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}:latest
41-
pulumi up --yes
48+
pulumi up --stack ExitoLab/todo-app/dev --yes --non-interactive
49+
env:
50+
PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}

todo-app/Pulumi.dev.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
config:
2+
todo-app:docker_image: 289940214902.dkr.ecr.us-east-1.amazonaws.com/todo_pulumi_docker_aws_lambda_api_gateway:a10b29b
3+
todo-app:environment: dev
4+
todo-app:region: us-east-1

todo-app/__main__.py

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,140 @@
1-
import pulumi
1+
import pulumi, json
22
import pulumi_aws as aws
33
from pulumi_docker import Image, DockerBuild
44
import pulumi_docker as docker
55

6-
# Step 1: Create an ECR repository
7-
docker_image = "289940214902.dkr.ecr.us-east-1.amazonaws.com/todo-app:v1.1"
6+
from pulumi import Config
7+
8+
# Create a config object to access configuration values
9+
config = pulumi.Config()
10+
11+
docker_image = config.get("docker_image")
12+
environment = config.get("environment")
13+
region = config.get("region")
14+
15+
aws.config.region = region
16+
17+
# First, create the DynamoDB table
18+
dynamodb_table = aws.dynamodb.Table(
19+
f"todo-{environment}",
20+
name=f"todo-{environment}",
21+
hash_key="id",
22+
range_key="timestamp",
23+
attributes=[
24+
aws.dynamodb.TableAttributeArgs(
25+
name="id",
26+
type="S"
27+
),
28+
aws.dynamodb.TableAttributeArgs(
29+
name="timestamp",
30+
type="N"
31+
),
32+
],
33+
billing_mode="PAY_PER_REQUEST",
34+
tags={
35+
"Environment": environment,
36+
"Created_By": "Pulumi"
37+
}
38+
)
839

940
# Create an IAM Role for the Lambda function
10-
lambda_role = aws.iam.Role("lambdaExecutionRole",
11-
assume_role_policy="""{
12-
"Version": "2012-10-17",
13-
"Statement": [
14-
{
15-
"Action": "sts:AssumeRole",
16-
"Principal": {
17-
"Service": "lambda.amazonaws.com"
18-
},
19-
"Effect": "Allow",
20-
"Sid": ""
21-
}
22-
]
23-
}"""
41+
# Create Lambda execution role
42+
lambda_role = aws.iam.Role(
43+
"lambdaExecutionRole",
44+
assume_role_policy=json.dumps({
45+
"Version": "2012-10-17",
46+
"Statement": [{
47+
"Action": "sts:AssumeRole",
48+
"Principal": {
49+
"Service": "lambda.amazonaws.com"
50+
},
51+
"Effect": "Allow",
52+
"Sid": ""
53+
}]
54+
})
2455
)
2556

26-
# Attach the basic execution policy to the role
27-
lambda_policy_attachment = aws.iam.RolePolicyAttachment("lambdaExecutionPolicy",
28-
role=lambda_role.name,
29-
policy_arn="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
57+
# Create inline policy for the role
58+
dynamodb_policy = aws.iam.RolePolicy(
59+
f"lambdaRolePolicy-{environment}",
60+
role=lambda_role.id,
61+
policy=pulumi.Output.json_dumps({
62+
"Version": "2012-10-17",
63+
"Statement": [
64+
{
65+
"Effect": "Allow",
66+
"Action": [
67+
"dynamodb:Scan",
68+
"dynamodb:PutItem",
69+
"dynamodb:GetItem",
70+
"dynamodb:UpdateItem",
71+
"dynamodb:DeleteItem",
72+
"dynamodb:Query"
73+
],
74+
"Resource": [
75+
dynamodb_table.arn,
76+
pulumi.Output.concat(dynamodb_table.arn, "/*")
77+
]
78+
},
79+
{
80+
"Effect": "Allow",
81+
"Action": [
82+
"logs:CreateLogGroup",
83+
"logs:CreateLogStream",
84+
"logs:PutLogEvents"
85+
],
86+
"Resource": "arn:aws:logs:*:*:*"
87+
}
88+
]
89+
})
3090
)
3191

3292
# Create a Lambda function using the Docker image
33-
lambda_function = aws.lambda_.Function("my-serverless-function",
34-
name="my-serverless-function",
35-
role=lambda_role.arn, # Make sure you have the correct IAM role
36-
package_type="Image", # Specify that this is a Docker image
37-
image_uri=docker_image, # Use the image name from the previous step
38-
memory_size=512, # Example memory size
39-
timeout=30 # Example timeout in seconds
93+
lambda_function = aws.lambda_.Function(
94+
f"my-serverless-function-{environment}",
95+
role=lambda_role.arn,
96+
package_type="Image",
97+
image_uri=docker_image,
98+
memory_size=512,
99+
timeout=30,
100+
opts=pulumi.ResourceOptions(depends_on=[lambda_role])
40101
)
41102

42103
# Create an API Gateway REST API
43-
api = aws.apigateway.RestApi("my-api",
104+
api = aws.apigateway.RestApi(f"my-api-{environment}",
44105
description="My serverless API")
45106

46107
# Create a catch-all resource for the API
47-
proxy_resource = aws.apigateway.Resource("proxy-resource",
108+
proxy_resource = aws.apigateway.Resource(f"proxy-resource-{environment}",
48109
rest_api=api.id,
49110
parent_id=api.root_resource_id,
50111
path_part="{proxy+}")
51112

52113
# Create a method for the proxy resource that allows any method
53-
method = aws.apigateway.Method("proxy-method",
114+
method = aws.apigateway.Method(f"proxy-method-{environment}",
54115
rest_api=api.id,
55116
resource_id=proxy_resource.id,
56117
http_method="ANY",
57118
authorization="NONE")
58119

59120
# Integration of Lambda with API Gateway using AWS_PROXY
60-
integration = aws.apigateway.Integration("proxy-integration",
121+
integration = aws.apigateway.Integration(f"proxy-integration-{environment}",
61122
rest_api=api.id,
62123
resource_id=proxy_resource.id,
63124
http_method=method.http_method,
64125
integration_http_method="POST",
65126
type="AWS_PROXY",
66127
uri=lambda_function.invoke_arn) # Ensure lambda_function is defined
67128

68-
69-
lambda_permission = aws.lambda_.Permission("api-gateway-lambda-permission",
129+
lambda_permission = aws.lambda_.Permission(f"api-gateway-lambda-permission-{environment}",
70130
action="lambda:InvokeFunction",
71131
function=lambda_function.name,
72132
principal="apigateway.amazonaws.com",
73133
source_arn=pulumi.Output.concat(api.execution_arn, "/*/*")
74134
)
75135

76-
77136
# Deployment of the API, explicitly depends on method and integration to avoid timing issues
78-
deployment = aws.apigateway.Deployment("api-deployment",
137+
deployment = aws.apigateway.Deployment(f"api-deployment-{environment}",
79138
rest_api=api.id,
80139
stage_name="dev",
81140
opts=pulumi.ResourceOptions(
@@ -90,8 +149,4 @@
90149

91150
pulumi.export("api_invoke_url", api_invoke_url)
92151

93-
94-
# # Output the invoke URL of the API
95-
# pulumi.export("api_invoke_url", pulumi.Output.all(api.id, aws.config.region).apply(lambda values: f"https://{values[0]}.execute-api.{values[1]}.amazonaws.com/dev/{proxy_resource.path_part}"))
96-
97152
# #How does Pulumi stores statesfile

todo-app/lambda_function/.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)