Skip to content

Commit bae6b76

Browse files
authored
Add support for CloudWatch output config in SSM RunCommand task (#420)
1 parent de2c8ba commit bae6b76

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "Add support for CloudWatch output config in SSM RunCommand task"
4+
}

src/tasks/SystemsManagerRunCommand/TaskOperations.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ export class TaskOperations {
3838
if (this.taskParameters.documentParameters) {
3939
request.Parameters = JSON.parse(this.taskParameters.documentParameters)
4040
}
41+
if (this.taskParameters.cloudWatchOutputEnabled) {
42+
request.CloudWatchOutputConfig = {
43+
CloudWatchOutputEnabled: true
44+
}
45+
46+
if (this.taskParameters.cloudWatchLogGroupName) {
47+
request.CloudWatchOutputConfig.CloudWatchLogGroupName = this.taskParameters.cloudWatchLogGroupName
48+
}
49+
}
4150

4251
switch (this.taskParameters.instanceSelector) {
4352
case fromInstanceIds:

src/tasks/SystemsManagerRunCommand/TaskParameters.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface TaskParameters {
2929
notificationType: string | undefined
3030
outputS3BucketName: string
3131
outputS3KeyPrefix: string
32+
cloudWatchOutputEnabled: boolean;
33+
cloudWatchLogGroupName: string;
3234
commandIdOutputVariable: string
3335
}
3436

@@ -48,6 +50,8 @@ export function buildTaskParameters(): TaskParameters {
4850
notificationType: getInputOptional('notificationType'),
4951
outputS3BucketName: getInputOrEmpty('outputS3BucketName'),
5052
outputS3KeyPrefix: getInputOrEmpty('outputS3KeyPrefix'),
53+
cloudWatchOutputEnabled: tl.getBoolInput('cloudWatchOutputEnabled'),
54+
cloudWatchLogGroupName: getInputOrEmpty('cloudWatchLogGroupName'),
5155
commandIdOutputVariable: getInputOrEmpty('commandIdOutputVariable'),
5256
instanceIds: [],
5357
instanceTags: [],

src/tasks/SystemsManagerRunCommand/task.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
"displayName": "S3 Output",
2727
"isExpanded": false
2828
},
29+
{
30+
"name": "cloudWatchOutput",
31+
"displayName": "CloudWatch Options",
32+
"isExpanded": false
33+
},
2934
{
3035
"name": "diagnostic",
3136
"displayName": "Diagnostic",
@@ -227,6 +232,25 @@
227232
"helpMarkDown": "The name of a variable that will contain the unique ID assigned to the command. The command ID can be used future references to the request.",
228233
"required": false
229234
},
235+
{
236+
"name": "cloudWatchOutputEnabled",
237+
"label": "Enable CloudWatch Logs",
238+
"type": "boolean",
239+
"required": false,
240+
"defaultValue": false,
241+
"groupName": "cloudWatchOutput",
242+
"helpMarkDown": "Enables Systems Manager to send command output to CloudWatch Logs."
243+
},
244+
{
245+
"name": "cloudWatchLogGroupName",
246+
"label": "CloudWatch Log Group Name",
247+
"type": "string",
248+
"required": false,
249+
"defaultValue": "",
250+
"groupName": "cloudWatchOutput",
251+
"visibleRule": "cloudWatchOutputEnabled = true",
252+
"helpMarkDown": "The name of the CloudWatch log group where you want to send command output. If you don’t specify a group name, Systems Manager automatically creates a log group for you. The log group uses the following naming format: <em>aws/ssm/<systems_manager_document_name></em>."
253+
},
230254
{
231255
"name": "logRequest",
232256
"type": "boolean",

tests/taskTests/systemsManagerRunCommand/systemsManagerRunCommand-test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const defaultTaskParameters: TaskParameters = {
3434
notificationType: '',
3535
outputS3BucketName: '',
3636
outputS3KeyPrefix: '',
37+
cloudWatchOutputEnabled: false,
38+
cloudWatchLogGroupName: '',
3739
commandIdOutputVariable: ''
3840
}
3941

@@ -132,6 +134,40 @@ describe('Systems Manager Run Command', () => {
132134
expect(ssm.sendCommand).toBeCalledTimes(1)
133135
})
134136

137+
test('Adds Cloudwatch output config if it exists', async () => {
138+
expect.assertions(2)
139+
const ssm = new SSM() as any
140+
ssm.sendCommand = jest.fn(args => {
141+
expect(args.CloudWatchOutputConfig.CloudWatchLogGroupName).toBe('cloudWatchLogGroupName')
142+
143+
return systemsManagerFails
144+
})
145+
const taskParameters: TaskParameters = { ...defaultTaskParameters, cloudWatchOutputEnabled: true }
146+
taskParameters.cloudWatchLogGroupName = 'cloudWatchLogGroupName'
147+
const taskOperations = new TaskOperations(ssm, taskParameters)
148+
try {
149+
await taskOperations.execute()
150+
} catch (e) {}
151+
expect(ssm.sendCommand).toBeCalledTimes(1)
152+
})
153+
154+
test('Do not add Cloudwatch output config if CloudWatch output is disabled', async () => {
155+
expect.assertions(2)
156+
const ssm = new SSM() as any
157+
ssm.sendCommand = jest.fn(args => {
158+
expect(args.CloudWatchOutputConfig).toBeFalsy()
159+
160+
return systemsManagerFails
161+
})
162+
const taskParameters: TaskParameters = { ...defaultTaskParameters }
163+
taskParameters.cloudWatchLogGroupName = 'cloudWatchLogGroupName'
164+
const taskOperations = new TaskOperations(ssm, taskParameters)
165+
try {
166+
await taskOperations.execute()
167+
} catch (e) {}
168+
expect(ssm.sendCommand).toBeCalledTimes(1)
169+
})
170+
135171
test('Happy path', async () => {
136172
expect.assertions(1)
137173
const ssm = new SSM() as any

0 commit comments

Comments
 (0)