AWSCDK,首次部署时运行代码,但之后不再运行
创始人
2024-09-24 03:30:53
0

问题可能是由于 AWS CDK 在部署期间使用 CloudFormation 来管理资源。当需求没有发生变化时,CloudFormation 不会更新资源,这可能会导致在之后的部署中不再运行代码。为了避免这种情况,可以强制 CloudFormation 更新资源。这可以通过在 AWS CDK 中添加自定义资源并将其用作占位符来完成。

以下是一个使用自定义资源的示例:

import * as cdk from 'aws-cdk-lib';
import * as cloudformation from 'aws-cdk-lib/aws-cloudformation';
import * as iam from 'aws-cdk-lib/aws-iam';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const resource = new cloudformation.CustomResource(this, 'MyResource', {
      provider: cloudformation.CustomResourceProvider.lambda(new MyLambda(this, 'MyLambda')),
      properties: {
        // Add any required properties here
      }
    });

    // Grant the IAM role used by CloudFormation to invoke the Lambda function
    resource.role?.addToPolicy(new iam.PolicyStatement({
      actions: ['lambda:InvokeFunction'],
      resources: [resource.provider.serviceToken],
      effect: iam.Effect.ALLOW,
    }));
  }
}

class MyLambda extends cdk.Construct implements cloudformation.ICustomResourceProvider {
  public readonly serviceToken: string;

  constructor(scope: cdk.Construct, id: string) {
    super(scope, id);

    // Define your Lambda function here
    const fn = new lambda.Function(this, 'MyFn', {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
      code: lambda.Code.fromAsset('./my-lambda'),
    });

    this.serviceToken = fn.functionArn;
  }

  public async onCreate(_event: cloudformation.Event): Promise {
    // Add code to run when the resource is created
    return { status: cloudformation.OperationStatus.SUCCESS };
  }
  
  public async onUpdate(_event: cloudformation.Event): Promise {
    // Add code to run when the resource is updated
    return { status: cloudformation.OperationStatus.SUCCESS };
  }

  public async onDelete(_event: cloudformation.Event): Promise {
    // Add code to run when the resource is deleted
    return { status: cloudformation.OperationStatus.SUCCESS };
  }
}

此代码创建了一个自定义资源,其中可以运行特定的代码

相关内容

热门资讯

开挂辅助安装"wep... 开挂辅助安装"wepoker有透视功能吗"开挂(透视)辅助插件(有挂存在) 了解更多开挂安装加(13...
开挂辅助安装"pok... 您好:pokemmo脚本手机版这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
开挂辅助插件"wep... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
开挂辅助神器"wpk... 【亲,wpk德州局透视 这款游戏可以开挂的,确实是有挂的,很多玩家在这款wpk德州局透视中打牌都会发...
开挂辅助神器"wep... wepoker透视app下载开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepoker透视app...
开挂辅助工具"hhp... 开挂辅助工具"hhpoker有没有辅助挂"开挂(透视)辅助下载(有挂解密);无需打开直接搜索加薇13...
开挂辅助平台"har... 【亲,hardrock辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款hardrock辅助中打...
开挂辅助神器"拱趴大... 开挂辅助神器"拱趴大菠萝挂"开挂(透视)辅助插件(真的有挂)《详细加薇136704302咨询》游戏特...
开挂辅助神器"wep... 开挂辅助神器"wepoker私人局怎么玩"开挂(透视)辅助脚本(有人有挂);无需打开直接搜索加(薇:...
开挂辅助安装"wep... 开挂辅助安装"wepoker黑侠破解"开挂(透视)辅助工具(有挂猫腻)《详细加薇136704302咨...