LoginSignup
6
0

More than 5 years have passed since last update.

AWS CDK単体でもデプロイできますが、CloudFormationの生成にも利用できます。
そうなってくると、ParametersやRefを使いたくなりますね。

cdk.CfnParameterを使って定義して、.refパラメタを使えば値を渡してやることができます。

SAMのハンドラーをParameterで選べるようにした例

import * as cdk from '@aws-cdk/cdk';
import sam = require('@aws-cdk/aws-sam')

export class CdkTestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const ParamHandler = new cdk.CfnParameter(this, 'handler', {
      type: 'String',
      description: 'Lambda handler',
      default: 'index.handler'
    })
    new sam.CfnFunction(
      this,
      'function',
      {
        handler: ParamHandler.ref,
        runtime: 'nodejs8.10',
        codeUri: 'src'
      }
    )
  }
}

生成されるCloudFormation

Transform: AWS::Serverless-2016-10-31
Parameters:
  handler:
    Type: String
    Default: index.handler
    Description: Lambda handler
Resources:
  function:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src
      Handler:
        Ref: handler
      Runtime: nodejs8.10
    Metadata:
      aws:cdk:path: CdkTestStack/function
6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0