-
Notifications
You must be signed in to change notification settings - Fork 9
/
bottlerocket-ecs-updater.yaml
265 lines (265 loc) · 9.6 KB
/
bottlerocket-ecs-updater.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
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
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Bottlerocket ECS updater automation & resources'
Parameters:
ClusterName:
Description: 'Name of ECS cluster to manage Bottlerocket instances in'
Type: String
Subnets:
Description: 'List of VPC Subnet IDs where the updater should run. The subnets must have a route to the Internet via an Internet Gateway.'
Type: List<AWS::EC2::Subnet::Id>
UpdaterImage:
Description: 'Bottlerocket updater container image'
Type: String
Default: 'public.ecr.aws/bottlerocket/bottlerocket-ecs-updater:v0.2.2'
LogGroupName:
Description: 'Log group name for Bottlerocket updater logs'
Type: String
ScheduleState:
Description: 'Schedule events rule state; allows disabling of scheduling'
Type: String
Default: 'ENABLED'
Resources:
ExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'ecs-tasks.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: CreateLogGroupPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
# Allows creating log group if it does not exist
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
Resource:
- 'arn:aws:logs:*:*:*'
Path: !Sub /${AWS::StackName}/
ManagedPolicyArns:
- !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
TaskRole:
Type: AWS::IAM::Role
Properties:
Description: 'Role allowing the Bottlerocket ECS Updater to manage Bottlerocket instances'
Path: !Sub '/${AWS::StackName}/'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: 'ecs-tasks.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: 'BottlerocketEcsUpdaterPolicy'
PolicyDocument:
Version: 2012-10-17
Statement:
# Allows listing all container instances in a cluster
- Effect: Allow
Action:
- 'ecs:ListContainerInstances'
Resource:
- !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}'
# Allows describe container instances to get ec2 instance ID and ecs attributes to filter Bottlerocket instances
# Allows list tasks to filter instances running standalone tasks
# Allows update container instance state for draining
# Allows describe tasks to identify tasks not started by service
- Effect: Allow
Action:
- 'ecs:DescribeContainerInstances'
- 'ecs:ListTasks'
- 'ecs:UpdateContainerInstancesState'
- 'ecs:DescribeTasks'
Resource: '*'
Condition:
ArnEquals:
ecs:cluster: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}'
# Allows ssm send command to make Bottlerocket update API calls
- Effect: Allow
Action:
- 'ssm:SendCommand'
Resource:
- !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:document/${UpdateCheckCommand}"
- !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:document/${UpdateApplyCommand}"
- !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:document/${RebootCommand}"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*"
# Allows get command invocation to get Bottlerocket API calls output
- Effect: Allow
Action:
- 'ssm:GetCommandInvocation'
Resource:
- !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:*"
# Allows checking the EC2 instance state after an update occurs
- Effect: Allow
Action:
- 'ec2:DescribeInstanceStatus'
Resource: '*'
UpdaterTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Cpu: "256"
Memory: "0.5GB"
ExecutionRoleArn: !GetAtt ExecutionRole.Arn
TaskRoleArn: !GetAtt TaskRole.Arn
ContainerDefinitions:
- Name: BottlerocketEcsUpdaterService
Image: !Ref UpdaterImage
Command:
- -cluster
- !Ref ClusterName
- -region
- !Ref AWS::Region
- -check-document
- !Ref UpdateCheckCommand
- -apply-document
- !Ref UpdateApplyCommand
- -reboot-document
- !Ref RebootCommand
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-create-group: 'true'
awslogs-region: !Ref AWS::Region
awslogs-group: !Ref LogGroupName
awslogs-stream-prefix: !Sub '/ecs/bottlerocket-updater/${ClusterName}'
BottlerocketUpdaterSchedule:
Type: AWS::Events::Rule
Properties:
Description: "Check for Bottlerocket updates on a schedule"
# Run Task every 12 hours
ScheduleExpression: "rate(12 hours)"
State: !Ref ScheduleState
Targets:
- Id: ecs-updater-fargate-task
RoleArn: !GetAtt CronRole.Arn
Arn: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}'
Input:
!Sub |
{
"containerOverrides": [
{
"name": "BottlerocketEcsUpdaterService",
"environment": [
{
"name" : "TASK_DEFINITION_ARN",
"value": "${UpdaterTaskDefinition}"
}
]
}
]
}
EcsParameters:
LaunchType: FARGATE
TaskCount: 1
TaskDefinitionArn: !Ref UpdaterTaskDefinition
NetworkConfiguration:
AwsVpcConfiguration:
# The Bottlerocket ECS Updater does not need a public IP for its operations. The public IP
# is only required to pull images from ECR as a Fargate task
AssignPublicIp: ENABLED
Subnets: !Ref Subnets
CronRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "events.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: !Sub '/${AWS::StackName}/'
Policies:
- PolicyName: "BottlerocketEcsUpdaterSchedulerPolicy"
PolicyDocument:
Statement:
- Effect: "Allow"
Condition:
ArnEquals:
ecs:cluster: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}'
Action: "ecs:RunTask"
Resource:
- !Ref UpdaterTaskDefinition
- Effect: "Allow"
Condition:
ArnEquals:
ecs:cluster: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}'
Action:
- "iam:PassRole"
Resource:
- !GetAtt TaskRole.Arn
- !GetAtt ExecutionRole.Arn
UpdateCheckCommand:
Type: AWS::SSM::Document
Properties:
DocumentType: Command
Content:
schemaVersion: "2.2"
description: "Bottlerocket - Check available updates"
mainSteps:
- action: "aws:runShellScript"
name: "CheckUpdate"
precondition:
StringEquals:
- platformType
- Linux
inputs:
timeoutSeconds: '1800'
runCommand:
- "apiclient update check"
UpdateApplyCommand:
Type: AWS::SSM::Document
Properties:
DocumentType: Command
Content:
schemaVersion: "2.2"
description: "Bottlerocket - Apply update"
mainSteps:
- action: "aws:runShellScript"
name: "ApplyUpdate"
precondition:
StringEquals:
- platformType
- Linux
inputs:
timeoutSeconds: '1800'
runCommand:
- "apiclient update apply"
RebootCommand:
Type: AWS::SSM::Document
Properties:
DocumentType: Command
Content:
schemaVersion: "2.2"
description: "Bottlerocket - Reboot"
mainSteps:
- action: "aws:runShellScript"
name: "Reboot"
precondition:
StringEquals:
- platformType
- Linux
inputs:
timeoutSeconds: '1800'
runCommand:
- "apiclient reboot"
Outputs:
UpdaterTaskDefinitionArn:
Description: 'Updater task definition ARN'
Value: !Ref UpdaterTaskDefinition
Export:
Name: !Sub "${AWS::StackName}:UpdaterTaskDefinition"