-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-cloudformation.yaml
More file actions
202 lines (185 loc) · 5.86 KB
/
Copy pathbasic-cloudformation.yaml
File metadata and controls
202 lines (185 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Basic CloudFormation template with common AWS resources'
Parameters:
Environment:
Type: String
Default: dev
AllowedValues:
- dev
- staging
- prod
Description: Environment name
Resources:
# S3 Bucket for storing application data
AppDataBucket:
Type: AWS::S3::Bucket
Properties:
# Note: Bucket names must be lowercase. Consider using a parameter or fixed prefix
# instead of AWS::StackName which may contain uppercase letters
BucketName: !Sub '${AWS::AccountId}-${AWS::Region}-data-${Environment}'
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
# DynamoDB Table for user data
UserTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${AWS::StackName}-users-${Environment}'
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: email
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: EmailIndex
KeySchema:
- AttributeName: email
KeyType: HASH
Projection:
ProjectionType: ALL
# Lambda Execution Role
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: DynamoDBAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Query
Resource:
- !GetAtt UserTable.Arn
- !Sub '${UserTable.Arn}/index/*'
- PolicyName: S3Access
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource: !Sub '${AppDataBucket.Arn}/*'
# Lambda Function for processing user data
UserProcessorFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-user-processor-${Environment}'
Runtime: python3.11
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Timeout: 60
MemorySize: 256
Environment:
Variables:
USER_TABLE_NAME: !Ref UserTable
BUCKET_NAME: !Ref AppDataBucket
ENVIRONMENT: !Ref Environment
Code:
ZipFile: |
import json
import os
import boto3
def handler(event, context):
# Sample Lambda function
print(f"Processing event: {json.dumps(event)}")
table_name = os.environ['USER_TABLE_NAME']
bucket_name = os.environ['BUCKET_NAME']
return {
'statusCode': 200,
'body': json.dumps({
'message': 'User processed successfully',
'table': table_name,
'bucket': bucket_name
})
}
# API Gateway for REST API
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub '${AWS::StackName}-api-${Environment}'
Description: REST API for user management
EndpointConfiguration:
Types:
- REGIONAL
# API Gateway Resource
UserResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref RestApi
ParentId: !GetAtt RestApi.RootResourceId
PathPart: users
# API Gateway Method
UserGetMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref RestApi
ResourceId: !Ref UserResource
HttpMethod: GET
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UserProcessorFunction.Arn}/invocations'
# Lambda Permission for API Gateway
ApiGatewayInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref UserProcessorFunction
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/*'
# API Gateway Deployment
ApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: UserGetMethod
Properties:
RestApiId: !Ref RestApi
StageName: !Ref Environment
Outputs:
BucketName:
Description: Name of the S3 bucket
Value: !Ref AppDataBucket
Export:
Name: !Sub '${AWS::StackName}-BucketName'
TableName:
Description: Name of the DynamoDB table
Value: !Ref UserTable
Export:
Name: !Sub '${AWS::StackName}-TableName'
ApiUrl:
Description: URL of the REST API
Value: !Sub 'https://${RestApi}.execute-api.${AWS::Region}.amazonaws.com/${Environment}'
Export:
Name: !Sub '${AWS::StackName}-ApiUrl'
LambdaFunctionArn:
Description: ARN of the Lambda function
Value: !GetAtt UserProcessorFunction.Arn
Export:
Name: !Sub '${AWS::StackName}-LambdaArn'