Guest User

Amplify custom resource

a guest
Jul 18th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
  2. import * as ev from '@aws-cdk/aws-events';
  3. import * as et from '@aws-cdk/aws-events-targets';
  4. import * as lambda from '@aws-cdk/aws-lambda';
  5. import * as ssm from '@aws-cdk/aws-ssm';
  6. import * as cdk from '@aws-cdk/core';
  7. import * as cr from '@aws-cdk/custom-resources';
  8.  
  9. import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref';
  10.  
  11. export class cdkStack extends cdk.Stack {
  12.   constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) {
  13.     super(scope, id, props);
  14.  
  15.     // CloudFormation complains if I move this in values-stack with
  16.     // "Parameter values specified for a template which does not require them."
  17.     new cdk.CfnParameter(this, 'env', {
  18.       type: 'String',
  19.       description: 'Current Amplify CLI env name',
  20.     });
  21.  
  22.     const busName = new ssm.StringParameter(this, `reservationbusname`, {
  23.       stringValue: `busname`,
  24.       dataType: ssm.ParameterDataType.TEXT,
  25.     }).stringValue;
  26.  
  27.     const ruleName = new ssm.StringParameter(this, `reservationrulename`, {
  28.       stringValue: `busrule`,
  29.       dataType: ssm.ParameterDataType.TEXT,
  30.     }).stringValue;
  31.  
  32.     const sourcePar = new ssm.StringParameter(this, `reservationbussourcename`, {
  33.       stringValue: `my.source`,
  34.       dataType: ssm.ParameterDataType.TEXT
  35.     });
  36.  
  37.     const source = sourcePar.stringValue;
  38.  
  39.     const deps: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
  40.       amplifyResourceProps.category,
  41.       amplifyResourceProps.resourceName,
  42.       [
  43.         { category: "function", resourceName: "lambda1" },
  44.         { category: "function", resourceName: "lambda2" }
  45.       ]
  46.     );
  47.  
  48.     const lambda1Arn = cdk.Fn.ref(deps.function.lambda1.Arn);
  49.     const lambda2Arn = cdk.Fn.ref(deps.function.lambda1.Arn);
  50.  
  51.     const eventBus = new ev.EventBus(this, `reservationbus`, {
  52.       eventBusName: busName
  53.     });
  54.  
  55.     const lambda1 = lambda.Function.fromFunctionArn(this,
  56.       `lambda1id`,
  57.       lambda1Arn);
  58.  
  59.     eventBus.grantPutEventsTo(lambda1);
  60.  
  61.     const lambda2 = lambda.Function.fromFunctionAttributes(this, `lambda2`, {
  62.       functionArn: lambda2Arn,
  63.       sameEnvironment: true
  64.     });
  65.  
  66.     const rule = new ev.Rule(this, `rule-id`, {
  67.       ruleName: ruleName,
  68.       eventBus: eventBus,
  69.       eventPattern: {
  70.         source: [source],
  71.       },
  72.       targets: [
  73.         new et.LambdaFunction(lambda1)
  74.       ],
  75.     });
  76.  
  77.     et.addLambdaPermission(rule, lambda1);
  78.  
  79.   }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment