1
- import pulumi
1
+ import pulumi , json
2
2
import pulumi_aws as aws
3
3
from pulumi_docker import Image , DockerBuild
4
4
import pulumi_docker as docker
5
5
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
+ )
8
39
9
40
# 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
+ })
24
55
)
25
56
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
+ })
30
90
)
31
91
32
92
# 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 ])
40
101
)
41
102
42
103
# Create an API Gateway REST API
43
- api = aws .apigateway .RestApi ("my-api" ,
104
+ api = aws .apigateway .RestApi (f "my-api- { environment } " ,
44
105
description = "My serverless API" )
45
106
46
107
# 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 } " ,
48
109
rest_api = api .id ,
49
110
parent_id = api .root_resource_id ,
50
111
path_part = "{proxy+}" )
51
112
52
113
# 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 } " ,
54
115
rest_api = api .id ,
55
116
resource_id = proxy_resource .id ,
56
117
http_method = "ANY" ,
57
118
authorization = "NONE" )
58
119
59
120
# 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 } " ,
61
122
rest_api = api .id ,
62
123
resource_id = proxy_resource .id ,
63
124
http_method = method .http_method ,
64
125
integration_http_method = "POST" ,
65
126
type = "AWS_PROXY" ,
66
127
uri = lambda_function .invoke_arn ) # Ensure lambda_function is defined
67
128
68
-
69
- lambda_permission = aws .lambda_ .Permission ("api-gateway-lambda-permission" ,
129
+ lambda_permission = aws .lambda_ .Permission (f"api-gateway-lambda-permission-{ environment } " ,
70
130
action = "lambda:InvokeFunction" ,
71
131
function = lambda_function .name ,
72
132
principal = "apigateway.amazonaws.com" ,
73
133
source_arn = pulumi .Output .concat (api .execution_arn , "/*/*" )
74
134
)
75
135
76
-
77
136
# 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 } " ,
79
138
rest_api = api .id ,
80
139
stage_name = "dev" ,
81
140
opts = pulumi .ResourceOptions (
90
149
91
150
pulumi .export ("api_invoke_url" , api_invoke_url )
92
151
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
-
97
152
# #How does Pulumi stores statesfile
0 commit comments