-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda-authorizer-cloudformation.yml
166 lines (152 loc) · 5.12 KB
/
lambda-authorizer-cloudformation.yml
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
Resources:
#### Lambda Authorizer ####
LambdaAuthorizerExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: lambda-authorizer-execution-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource: '*'
PolicyName: lambda-authorizer-execution-policy
LambdaAuthorizerFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: lambda-authorizer
Handler: index.handler
Runtime: nodejs12.x
Role: !GetAtt LambdaAuthorizerExecutionRole.Arn
Code:
ZipFile: !Sub |
exports.handler = function(event, context) {
console.log('Upload built lambda authorizer code & update nodejs version to nodejs14.x');
};
LambdaApiGatewayPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt LambdaAuthorizerFunction.Arn
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
LambdaAuthorizerLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub '/aws/lambda/${LambdaAuthorizerFunction}'
RetentionInDays: 1
#### ####
#### API Gateway ####
APIGatewayHttpApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: sample-secured-app-gateway
Description: Sample API using the lambda authorizer
ProtocolType: HTTP
APIGatewayLambdaIntegration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref APIGatewayHttpApi
IntegrationType: HTTP_PROXY
IntegrationMethod: "ANY"
IntegrationUri: "https://google.com"
PayloadFormatVersion: 1.0
APIGatewayAuthorizer:
Type: AWS::ApiGatewayV2::Authorizer
Properties:
ApiId: !Ref APIGatewayHttpApi
AuthorizerType: REQUEST
AuthorizerPayloadFormatVersion: 2.0
AuthorizerUri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaAuthorizerFunction.Arn}/invocations'
IdentitySource:
- "$request.header.Authorization"
Name: LambdaAuthorizer
APIGatewayGetRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref APIGatewayHttpApi
RouteKey: 'GET /sample'
AuthorizationType: CUSTOM
AuthorizerId: !Ref APIGatewayAuthorizer
Target: !Sub 'integrations/${APIGatewayLambdaIntegration}'
APIGatewayLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: api-gateway-log-grp
RetentionInDays: 1
APIGatewayDevStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: Dev
Description: Dev Stage
AccessLogSettings:
DestinationArn: !GetAtt APIGatewayLogGroup.Arn
Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength", "integrationError":"$context.integrationErrorMessage" }'
AutoDeploy: true
ApiId: !Ref APIGatewayHttpApi
#### ####
#### S3 Website ####
WebAppS3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName: sample-secured-web-app-s3
WebAppS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebAppS3Bucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- s3:GetObject
Principal:
CanonicalUser: !GetAtt WebAppAccessIdentity.S3CanonicalUserId
Effect: Allow
Resource: !Sub '${WebAppS3Bucket.Arn}/*'
WebAppAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: sample-secured-web-app-cloudfront-access-identity
WebAppDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: !GetAtt WebAppS3Bucket.RegionalDomainName
Id: sampleSecuredWebAppBucket
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${WebAppAccessIdentity.Id}'
Enabled: true
DefaultRootObject: index.html
HttpVersion: http2
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
Compress: true
TargetOriginId: sampleSecuredWebAppBucket
ForwardedValues:
QueryString: false
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_100
#### ####