Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const aws = require('aws-sdk');
- const s3 = new aws.S3();
- const { configure, sendSuccess, sendFailure, sendResponse, LOG_VERBOSE } = require('./cfn-custom-resource.js');
- configure({ logLevel: LOG_VERBOSE });
- function generatePhysicalId(resourceProperties) {
- return `cfexample-${resourceProperties.BucketName}-${resourceProperties.Countdown}`;
- }
- async function createObjectResource(bucketName, countdown) {
- let data = '';
- for (let i=0; i < countdown; i++) {
- data = data + i + '\r\n';
- }
- const objectKey = `data-${countdown}.txt`;
- const s3Path = `s3://${bucketName}/${objectKey}`;
- console.log(`Creating objetct on S3: ${s3Path}.`);
- const params = {
- Bucket : bucketName,
- Key : objectKey,
- Body : data
- };
- const result = await s3.putObject(params).promise();
- return {
- props : {
- DataFile : s3Path,
- Metadata : JSON.stringify(result)
- }
- };
- }
- async function deleteObjectResource(bucketName, countdown) {
- let objectKey = `data-${countdown}.txt`;
- const s3Path = `s3://${bucketName}/${objectKey}`;
- console.log(`Deleting objetct on S3: ${s3Path}.`);
- const params = {
- Bucket : bucketName,
- Key : objectKey
- };
- const result = await s3.deleteObject(params).promise();
- return {
- props : {
- DataFile : `s3://${bucketName}/${objectKey}`,
- Metadata : JSON.stringify(result)
- }
- };
- }
- exports.handler = async (event) => {
- console.log(`Event payload: ${JSON.stringify(event)}.`);
- const physicalResourceId =
- generatePhysicalId(event.ResourceProperties);
- try {
- let result;
- if (event.RequestType === 'Create') {
- console.info(`New Create event received.`);
- result = await createObjectResource(event.ResourceProperties.BucketName, event.ResourceProperties.Countdown);
- } else if (event.RequestType === 'Update') {
- console.info(`New Update event received.`);
- // Only the creation of the new object is required: by changing the physical name of the
- // resource, a Delete event for the old version will be triggered
- result = await createObjectResource(event.ResourceProperties.BucketName, event.ResourceProperties.Countdown);
- } else if (event.RequestType === 'Delete') {
- console.info(`New Delete event received.`);
- result = await deleteObjectResource(event.ResourceProperties.BucketName, event.ResourceProperties.Countdown);
- } else {
- throw `Not supported request type: ${event.RequestType}.`;
- }
- console.log(`Operation completed succesfully. Notifying Cloudformation.`);
- await sendSuccess(physicalResourceId, result, event);
- } catch (err) {
- console.error(`Failed to execute the request. Event <<${JSON.stringify(event)}>> Error <<${JSON.stringify(err)}>>`);
- await sendFailure(physicalResourceId, {Error : err}, event);
- }
- };
- /*
- - Create the bucket (custom-resource-example) or delete its content if exists
- - Create the lambda
- - Make https://github.com/jgriepentrog/cfn-custom-resource available as file
- - Provide permissions to write to s3
- - Update the cloudformation templates with the ARN of the lambda
- - Update the cloudformation templates with the name of the bucket
- */
- /*
- cat << EOF > v1.yaml
- AWSTemplateFormatVersion: 2010-09-09
- Resources:
- CountdownResource:
- Type: 'Custom::CountdownResource'
- Version: '1.0'
- Properties:
- ServiceToken: arn:aws:lambda:eu-west-1:436628946705:function:cf-custom-resource-example
- BucketName : custom-resource-example
- Countdown : 100
- EOF
- cat << EOF > v2.yaml
- AWSTemplateFormatVersion: 2010-09-09
- Resources:
- CountdownResource:
- Type: 'Custom::CountdownResource'
- Version: '1.0'
- Properties:
- ServiceToken: arn:aws:lambda:eu-west-1:436628946705:function:cf-custom-resource-example
- BucketName : custom-resource-example
- Countdown : 50
- EOF
- // Open the lambda logs
- aws s3 ls s3://custom-resource-example
- aws cloudformation create-stack --stack-name cr-demo --template-body file://v1.yaml
- aws s3 ls s3://custom-resource-example
- aws cloudformation create-change-set --stack-name cr-demo --change-set-name cr-demo-1-2 --template-body file://v2.yaml
- aws cloudformation describe-change-set --stack-name cr-demo --change-set-name cr-demo-1-2
- aws cloudformation update-stack --stack-name cr-demo --template-body file://v2.yaml
- aws s3 ls s3://custom-resource-example
- aws cloudformation delete-stack --stack-name cr-demo
- aws s3 ls s3://custom-resource-example
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement