-
Notifications
You must be signed in to change notification settings - Fork 27
/
prod-deploy.yaml
185 lines (171 loc) · 5.9 KB
/
prod-deploy.yaml
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
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "AWS CloudFormation Template to deploy Serverless UI Testing (SUIT) demo application"
Parameters:
RepositoryName:
Description: Enter the name of the container to be used
Type: 'AWS::SSM::Parameter::Value<String>'
Default: WebAppSourceRepo
Resources:
AmplifyAccessRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub SUIT-${AWS::StackName}-Prod-AmplifyRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- amplify.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
-
PolicyName: !Sub SUIT-${AWS::StackName}-Prod-AmplifyRole-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "codecommit:GitPull"
Resource:
- !Sub arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${RepositoryName}
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/amplify/*
ProdApp:
Type: AWS::Amplify::App
Properties:
Name: ProdAppWebsite
Repository: !Sub https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${RepositoryName}
BuildSpec: |
version: 1
applications:
- frontend:
phases:
build:
commands: []
artifacts:
baseDirectory: /
files:
- '**/*'
cache:
paths: []
appRoot: website
IAMServiceRole: !GetAtt AmplifyAccessRole.Arn
CustomRules:
- Target: /index.html
Source: "/<*>"
Status: "404-200"
Tags:
- Key: Application
Value: !Ref AWS::StackId
- Key: Name
Value: ProdAppWebsite
ProdAppBranch:
Type: AWS::Amplify::Branch
Properties:
AppId: !GetAtt ProdApp.AppId
BranchName: master
Description: Master branch for App
EnableAutoBuild: true
Stage: PRODUCTION
Tags:
- Key: Application
Value: !Ref AWS::StackId
- Key: Name
Value: ProdAppBranch
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub SUIT-${AWS::StackName}-Prod-LambdaExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
-
PolicyName: !Sub SUIT-${AWS::StackName}-Prod-LambdaExecutionRole-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "amplify:StartJob"
Resource:
- !Sub arn:aws:amplify:${AWS::Region}:${AWS::AccountId}:apps/${ProdApp.AppId}/branches/*
TriggerDeploymentLambda:
Type: "AWS::Lambda::Function"
Properties:
Code:
ZipFile: |
import json
import boto3
import cfnresponse
amplify = boto3.client('amplify')
def lambda_handler(event, context):
try:
if event['RequestType'] == 'Delete' or event['RequestType'] == 'Change':
cfnresponse.send(event,
context,
cfnresponse.SUCCESS,
{})
return 0
elif event['RequestType'] == 'Create':
amplify.start_job(appId=event['ResourceProperties']['appId'],
branchName=event['ResourceProperties']['branchName'],
jobType='RELEASE')
responseData = {'Success': 'Successfully triggered deployment'}
cfnresponse.send(event,
context,
cfnresponse.SUCCESS,
responseData)
except Exception as e:
print('Received client error: %s' % e)
responseData = {'Failed': 'Received client error: %s' % e}
cfnresponse.send(event,
context,
cfnresponse.SUCCESS,
responseData)
Description: Lambda function that will start Amplify deployment.
FunctionName: !Sub SUIT-${AWS::StackName}-Prod-DeployAmplifyApp
Handler: index.lambda_handler
Role : !GetAtt LambdaExecutionRole.Arn
Runtime: python3.8
Timeout: 85
TriggerDeployment:
Type: Custom::TriggerDeployment
Properties:
ServiceToken: !GetAtt [ TriggerDeploymentLambda, Arn ]
appId: !GetAtt ProdApp.AppId
branchName: !GetAtt ProdAppBranch.BranchName
ProdAppDomainParameter:
Type: AWS::SSM::Parameter
Properties:
Name: ProdAppURL
Type: String
Value: !Sub https://master.${ProdApp.DefaultDomain}
Description: URL of production website
Outputs:
ProdWebsite:
Description: "URL of Prod Website"
Value: !Sub https://master.${ProdApp.DefaultDomain}