forked from aws/aws-connected-device-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn-provisioning.yml
295 lines (266 loc) · 9.78 KB
/
cfn-provisioning.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
---
#-----------------------------------------------------------------------------------------------------------------------
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
# with the License. A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#-----------------------------------------------------------------------------------------------------------------------
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CDF Provisioning Service
Globals:
Api:
OpenApiVersion: 3.0.1
Parameters:
ApplicationConfigurationOverride:
Description: This allows you to override any application configuration. It should consists of a text-based content with a structure and syntax comprising key–value pairs for properties. Any configurations contained in this will override the configurations found and merged from the default .env files.
Type: String
Environment:
Description: Name of environment. Used to name the created resources.
Type: String
MinLength: 1
TemplateSnippetS3UriBase:
Description: |
S3 uri of directory where template snippets are stored for the account.
Type: String
MinLength: 1
AuthType:
Description: Authorization type to apply to the API gateway endpoints
Type: String
Default: None
AllowedValues:
- None
- Private
- Cognito
- LambdaRequest
- LambdaToken
- ApiKey
- IAM
MinLength: 1
ApiGatewayDefinitionTemplate:
Description: |
Name of the API Gateway Cloudformation definition along with the authorization method to use. Use one of the provided
templates to implement no auth, private, api key, lambda request, lamdba token, or Cognito auth, or modify one to meet your own
authentization requirements. The template must exist within the provided TemplateSnippetS3UriBase location.
Type: String
MinLength: 1
VpcId:
Description: ID of VPC to deploy the API into. Only required if AuthType = 'Private'.
Type: String
CDFSecurityGroupId:
Description: ID of an existing CDF security group to deploy the API into. Only required if AuthType = 'Private'.
Type: String
PrivateSubNetIds:
Description: Comma delimited list of private subnetIds to deploy the API into. Only required if AuthType = 'Private'.
Type: CommaDelimitedList
PrivateApiGatewayVPCEndpoint:
Description: VPC endpoint. Only required if AuthType = 'Private'.
Type: String
CognitoUserPoolArn:
Description: Cognito user pool arn. Only required if AuthType is set to 'Cognito'.
Type: String
Default: 'N/A'
AuthorizerFunctionArn:
Description: Lambda authorizer function arn. Only required if AuthType is set to 'LambdaRequest' or 'LambdaToken'.
Type: String
Default: 'N/A'
KmsKeyId:
Description: The KMS key ID used to encrypt SSM parameters
Type: String
OpenSslLambdaLayerArn:
Description: OpenSSL lambda layer ARN
Type: String
MinLength: 1
BucketName:
Description: Name of bucket to store command information.
Type: String
MinLength: 1
CustomResourceLambdaArn:
Description: Custom resource lambda arn
Type: String
MinLength: 1
EnableApiGatewayAccessLogs:
Description: Enales API gateway Access Logging, defaults to false if not specified.
Type: String
Default: 'false'
AllowedValues:
- 'true'
- 'false'
MinLength: 1
ACMPCACrossAccountRoleArn:
Description: If ACM PCA functionality is to be used, and a ACM PCA is located within a different AWS Account, specify the cross-account IAM Role ARN.
Type: String
Default: ''
Conditions:
KmsKeyIdProvided: !Not [!Equals [!Ref KmsKeyId, '']]
DeployInVPC: !Not [!Equals [!Ref VpcId, 'N/A']]
DeployWithLambdaAuth:
!Or [!Equals [!Ref AuthType, 'LambdaRequest'], !Equals [!Ref AuthType, 'LambdaToken']]
EnableApiGatewayAccessLogs: !Equals [!Ref EnableApiGatewayAccessLogs, 'true']
ACMPCACrossAccountAccessRequired: !Not [!Equals [!Ref ACMPCACrossAccountRoleArn, '']]
Resources:
ApiGatewayApi:
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: !Sub '${TemplateSnippetS3UriBase}${ApiGatewayDefinitionTemplate}'
DependsOn:
- LambdaFunction
ApiGatewayAuthorizerInvokeRole:
Condition: DeployWithLambdaAuth
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: !Sub '${TemplateSnippetS3UriBase}cfn-role-lambdaRequestAuthInvokerRole.yaml'
DependsOn:
- LambdaFunction
ApiGatewayAccessLogGroup:
Condition: EnableApiGatewayAccessLogs
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !Sub 'cdf-provisioning-apigatewayaccesslogs-${Environment}'
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- iot.amazonaws.com
Action: sts:AssumeRole
Path: '/'
ManagedPolicyArns:
- !If [KmsKeyIdProvided, !Ref SSMPolicy, !Ref 'AWS::NoValue']
- !Ref BulkProvisioningPolicy
- !Ref ACMPolicy
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/AWSIoTConfigAccess
BulkProvisioningPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: 'cdf-provisioning policy for enabling bulk provisioning'
Path: '/cdf/provisioning/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'iam:PassRole'
Effect: Allow
Resource: !Sub 'arn:aws:iam::${AWS::AccountId}:role/cdf-provisioning-*'
- Action:
- 'iot:RegisterCertificateWithoutCA'
Effect: Allow
Resource: !Sub '*'
SSMPolicy:
Type: AWS::IAM::ManagedPolicy
Condition: KmsKeyIdProvided
Properties:
Description: 'cdf-provisioning policy for accessing SSM and KMS'
Path: '/cdf/provisioning/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'ssm:GetParameter*'
Effect: Allow
Resource: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/cdf-ca-key-*'
- Action:
- 'kms:Decrypt'
Effect: Allow
Resource: !Sub 'arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${KmsKeyId}'
ACMPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: 'cdf-bulkcerts policy for accessing ACM PCA'
Path: '/cdf/provisioning/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'acm-pca:IssueCertificate'
- 'acm-pca:GetCertificate'
Effect: Allow
Resource: '*'
# optional cross-account role access for ACM PCA
- Fn::If:
- ACMPCACrossAccountAccessRequired
- !Ref ACMPCACrossAccountRoleArn
- Ref: AWS::NoValue
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'cdf-provisioning-rest-${Environment}'
CodeUri: ../bundle.zip
Handler: lambda_proxy.handler
Layers:
- !Ref OpenSslLambdaLayerArn
MemorySize: 512
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs18.x
AutoPublishAlias: live
Timeout: 60
Environment:
Variables:
APP_CONFIG: !Ref ApplicationConfigurationOverride
APP_CONFIG_DIR: 'config'
AWS_ACCOUNT_ID: !Ref AWS::AccountId
AWS_S3_ROLE_ARN: !GetAtt LambdaExecutionRole.Arn
AWS_S3_TEMPLATES_BUCKET: !Ref BucketName
AWS_S3_BULKREQUESTS_BUCKET: !Ref BucketName
ACM_PCA_CROSS_ACCOUNT_ROLE_ARN: !Ref ACMPCACrossAccountRoleArn
Tracing: Active
VpcConfig:
Fn::If:
- DeployInVPC
- SubnetIds: !Ref PrivateSubNetIds
SecurityGroupIds:
- !Ref CDFSecurityGroupId
- Ref: AWS::NoValue
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /{proxy+}
Method: ANY
DependsOn:
- LambdaExecutionRole
Outputs:
RestApiFunctionName:
Description: Provisioning REST API lambda function name
Value: !Ref LambdaFunction
Export:
Name: !Sub 'cdf-provisioning-${Environment}-restApiFunctionName'
LambdaExecutionRoleArn:
Description: Provisioning REST API lambda function execution role
Value: !GetAtt LambdaExecutionRole.Arn
Export:
Name: !Sub 'cdf-provisioning-${Environment}-lambdaExecutionRoleArn'
ApiGatewayUrl:
Description: Provisioning REST API URL
Value: !Sub 'https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/Prod'
Export:
Name: !Sub 'cdf-provisioning-${Environment}-apigatewayurl'
ApiGatewayHost:
Description: Provisioning REST API host
Value: !Sub '${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com'
Export:
Name: !Sub 'cdf-provisioning-${Environment}-apigatewayhost'