Created
April 3, 2020 01:41
-
-
Save PopeFelix/2299c4fd32fadadd9c2f0334f36fe188 to your computer and use it in GitHub Desktop.
Custom CFN resource in Serverless
This file contains hidden or 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
| const { customResourceHelper, ResourceHandler, ResourceHandlerReturn } = require('custom-resource-helper') | |
| module.exports.handler = customResourceHelper(() => ({ | |
| onCreate: async (event, context, logger) => { | |
| // Place your code to handle Create events here. | |
| const physicalResourceId = 'myResourceId' | |
| const responseData = {} | |
| console.log('CREATE event: ', event) | |
| console.log('Logger: ', logger) | |
| return { | |
| physicalResourceId, | |
| responseData | |
| } | |
| }, | |
| onUpdate: async (event, context, logger) => { | |
| // Place your code to handle Update events here. | |
| const physicalResourceId = event.PhysicalResourceId | |
| const responseData = {} | |
| console.log('UPDATE event: ', event) | |
| console.log('Logger: ', logger) | |
| logger.info({ message: 'Foo bar baz' }) | |
| try { | |
| logger.info('blah blah blah') | |
| } catch (e) { | |
| console.warn('Looks like logging simple strings doesn\'t work') | |
| } | |
| try { | |
| logger.log({ message: 'blah blah blah' }) | |
| } catch (e) { | |
| console.warn('Looks like logging objects via logger.log doesn\'t work') | |
| } | |
| return { | |
| physicalResourceId, | |
| responseData | |
| } | |
| }, | |
| onDelete: async (event, context, logger) => { | |
| // Place your code to handle Delete events here | |
| } | |
| }) | |
| /* optional: customLogFactory */ | |
| ) |
This file contains hidden or 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
| service: custom-resource-example | |
| plugins: | |
| - serverless-plugin-custom-roles | |
| - serverless-webpack | |
| - serverless-offline # Execute lambdas locally for testing | |
| provider: | |
| name: aws | |
| stage: dev | |
| region: us-east-1 | |
| functions: | |
| customResource: | |
| handler: src/customResource.handler | |
| runtime: nodejs12.x | |
| resources: | |
| Resources: | |
| TestCustomResource: | |
| Type: Custom::TestResource | |
| Properties: | |
| LogLevel: "debug" | |
| ServiceToken: !GetAtt CustomResourceLambdaFunction.Arn | |
| DBProxyName: ${self:custom.namingPrefix}-proxy-1 | |
| EngineFamily: MYSQL | |
| Auth: | |
| Scheme: SECRETS | |
| SecretId: DatabaseSecret | |
| IAMAuthRequired: true | |
| RoleArn: DatabaseSecretAccessRoleArn | |
| DBSubnetGroupName: ${self:custom.vpc.subnetGroupName} | |
| VpcSecurityGroupId: !Ref DatabaseSecurityGroup | |
| DBClusterIdentifier: !Ref DatabaseCluster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment