代码之家  ›  专栏  ›  技术社区  ›  ataravati

使用AWS Amplify和CDK创建自定义资源

  •  0
  • ataravati  · 技术社区  · 3 年前

    我正在尝试使用AWS CDK在AWS Amplify中创建一个自定义资源。而且,我正在尝试使用现有的lambda函数作为提供程序事件处理程序。

    当我这样做的时候 amplify push 资源创建失败,没有任何有用的信息。我在这里做错了什么?如何解决此问题?

    import * as cdk from '@aws-cdk/core';
    import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
    import * as cr from "@aws-cdk/custom-resources";
    import * as logs from "@aws-cdk/aws-logs";
    import * as lambda from '@aws-cdk/aws-lambda';
    
    import { AmplifyDependentResourcesAttributes } from "../../types/amplify-dependent-resources-ref"
    
    export class cdkStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) {
        super(scope, id, props);
        /* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
        new cdk.CfnParameter(this, 'env', {
          type: 'String',
          description: 'Current Amplify CLI env name',
        });
    
        const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
          amplifyResourceProps.category,
          amplifyResourceProps.resourceName,
          [{
            category: "function",
            resourceName: "myFunction" 
          }] 
        );
    
    
        const myFunctionArn = cdk.Fn.ref(dependencies.function.myFunction.Arn);
        const importedLambda = lambda.Function.fromFunctionArn(this, "importedLambda", myFunctionArn);
        
        const provider = new cr.Provider(this, "MyCustomResourceProvider", {
          onEventHandler: importedLambda,
          logRetention: logs.RetentionDays.ONE_DAY,
        })
    
        new cdk.CustomResource(this, "MyCustomResource", {
          serviceToken: provider.serviceToken
        })
      }
    }
    

    以下是我得到的错误:

    CREATE_FAILED custommyCustomResourceXXXX AWS::CloudFormation::Stack参数:[AssetParametersXXXX,…..]必须有值。

    0 回复  |  直到 3 年前
        1
  •  2
  •   ataravati    3 年前

    我收到了AWS支持团队的回复。它看起来像 AssetParameters 错误是由于Amplify CLI当前不支持Amplify CLI中自定义资源类别内的自定义资源提供程序的高级构造。应通过以下方式创建资源:

    import * as cdk from '@aws-cdk/core';
    import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
    import * as lambda from '@aws-cdk/aws-lambda';
    
    import { AmplifyDependentResourcesAttributes } from "../../types/amplify-dependent-resources-ref"
    
    export class cdkStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) {
        super(scope, id, props);
        /* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
        new cdk.CfnParameter(this, 'env', {
          type: 'String',
          description: 'Current Amplify CLI env name',
        });
    
        const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
          amplifyResourceProps.category,
          amplifyResourceProps.resourceName,
          [{
            category: "function",
            resourceName: "myFunction" 
          }] 
        );
    
    
        const myFunctionArn = cdk.Fn.ref(dependencies.function.myFunction.Arn);
        const importedLambda = lambda.Function.fromFunctionArn(this, "importedLambda", myFunctionArn);
        
        new cdk.CustomResource(this, "MyCustomResource", {
          serviceToken: importedLambda.functionArn
        })
      }
    }
    
    推荐文章