Forked from guillaumesmo/custom-task-definition.yml
Created
September 25, 2020 07:38
-
-
Save nom3ad/07cd8260469b3e6d07fa71cc1190fc27 to your computer and use it in GitHub Desktop.
CloudFormation Custom Task Definition POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sources: | |
# https://cloudonaut.io/how-to-create-a-customized-cloudwatch-dashboard-with-cloudformation/ | |
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html | |
# https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ECS.html | |
Resources: | |
CustomTaskDefinition: | |
Type: 'Custom::TaskDefinition' | |
Version: '1.0' | |
Properties: | |
ServiceToken: !GetAtt 'CustomResourceFunction.Arn' | |
TaskDefinition: | | |
{ | |
containerDefinitions: [ | |
{ | |
name: "sleep", | |
image: "busybox", | |
command: [ | |
"sleep", | |
"360" | |
], | |
mountPoints: [ | |
{sourceVolume: "efs", containerPath: "/efs"} | |
] | |
} | |
], | |
family: "sleep360", | |
taskRoleArn: "", // required for EFS permissions | |
cpu: "256", | |
memory: "512", | |
networkMode: "awsvpc", | |
volumes: [ | |
{ | |
name: "efs", | |
efsVolumeConfiguration: { | |
fileSystemId: "" // required for EFS | |
} | |
} | |
] | |
} | |
CustomResourceFunction: | |
Type: 'AWS::Lambda::Function' | |
Properties: | |
Code: | |
ZipFile: | | |
const aws = require('aws-sdk') | |
const response = require('cfn-response') | |
const ecs = new aws.ECS({apiVersion: '2014-11-13'}) | |
exports.handler = function(event, context) { | |
console.log(`AWS SDK Version: ${aws.VERSION}`) | |
console.log("REQUEST RECEIVED:\n" + JSON.stringify(event)) | |
if (event.RequestType === 'Create' || event.RequestType === 'Update') { | |
ecs.registerTaskDefinition(eval(`(${event.ResourceProperties.TaskDefinition})`)) | |
.promise() | |
.then(data => { | |
console.log(`Created/Updated task definition ${data.taskDefinition.taskDefinitionArn}`) | |
response.send(event, context, response.SUCCESS, {}, data.taskDefinition.taskDefinitionArn) | |
}) | |
.catch(err => { | |
console.error(err); | |
response.send(event, context, response.FAILED) | |
}) | |
} else if (event.RequestType === 'Delete') { | |
ecs.deregisterTaskDefinition({taskDefinition: event.PhysicalResourceId}) | |
.promise() | |
.then(data => { | |
console.log(`Removed task definition ${event.PhysicalResourceId}`) | |
response.send(event, context, response.SUCCESS) | |
}) | |
.catch(err => { | |
if (err.code === 'InvalidParameterException') { | |
console.log(`Task definition: ${event.PhysicalResourceId} does not exist. Skipping deletion.`) | |
response.send(event, context, response.SUCCESS) | |
} else { | |
console.error(err) | |
response.send(event, context, response.FAILED) | |
} | |
}) | |
} else { | |
console.error(`Unsupported request type: ${event.RequestType}`) | |
response.send(event, context, response.FAILED) | |
} | |
} | |
Handler: 'index.handler' | |
MemorySize: 128 | |
Role: !GetAtt 'CustomResourceRole.Arn' | |
Runtime: 'nodejs12.x' | |
Timeout: 30 | |
CustomResourceRole: | |
Type: 'AWS::IAM::Role' | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: 'lambda.amazonaws.com' | |
Action: 'sts:AssumeRole' | |
Policies: | |
- PolicyName: 'customresource' | |
PolicyDocument: | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'ecs:DeregisterTaskDefinition' | |
- 'ecs:RegisterTaskDefinition' | |
Resource: '*' | |
- Effect: Allow | |
Action: | |
- 'logs:CreateLogGroup' | |
- 'logs:CreateLogStream' | |
- 'logs:PutLogEvents' | |
Resource: '*' | |
- Effect: Allow | |
Action: | |
- 'iam:PassRole' | |
Resource: '*' # replace with value of taskRoleArn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment