Skip to content

Latest commit

 

History

History
118 lines (111 loc) · 3.86 KB

README.md

File metadata and controls

118 lines (111 loc) · 3.86 KB

S001: How to echo value in AWS CloudFormation?

new CfnOutput(this, 'output', { value: 'hello world' });

more details S001-Hello-World.ts

S002: How to set Instance.MachineImage from CfnMapping.FindInMap(“MappingName”, Aws.REGION)?

This is originally discussed at https://stackoverflow.com/q/60645254/4108187 and it's going to be supported from cdk side aws/aws-cdk#12546

class MyImage implements ec2.IMachineImage {
  private mapping: { [k1: string]: { [k2: string]: any } } = {};
  constructor(readonly amiMap: { [region: string]: string }) {
    for (const [region, ami] of Object.entries(amiMap)) {
      this.mapping[region] = { ami };
    }
  }
  public getImage(parent: Construct): ec2.MachineImageConfig {
    const amiMap = new CfnMapping(parent, 'AmiMap', { mapping: this.mapping });
    return {
      imageId: amiMap.findInMap(Aws.REGION, 'ami'),
      userData: ec2.UserData.forLinux(),
      osType: ec2.OperatingSystemType.LINUX,
    };
  }
}
    new ec2.Instance(this, 'Instance', {
      vpc: new ec2.Vpc(this, 'VPC'),
      instanceType: new ec2.InstanceType('t2.micro'),
      machineImage: new MyImage({
        'cn-north-1': 'ami-cn-north-1',
        'cn-northwest-1': 'ami-cn-northwest-1',
      }),
    });

more details S002-Use-CfnMapping-in-an-agnostic-stack-for-GenericMachineImage.ts

S003: Create VPC on demand

/**
 * Create or import VPC
 * @param scope the cdk scope
 */
function getOrCreateVpc(scope: Construct): ec2.IVpc {
  // use an existing vpc or create a new one
  return scope.node.tryGetContext('use_default_vpc') === '1' ?
    ec2.Vpc.fromLookup(scope, 'Vpc', { isDefault: true }) :
    scope.node.tryGetContext('use_vpc_id') ?
      ec2.Vpc.fromLookup(scope, 'Vpc', { vpcId: scope.node.tryGetContext('use_vpc_id') }) :
      new ec2.Vpc(scope, 'Vpc', { maxAzs: 3, natGateways: 1 });
}

more details S003-Create-VPC-on-demand.ts

S004: Do not hardcode env

Don’t specify env with account and region like below that will generate account/region hardcode in CloudFormation template.

const app = new App();
// Don't
new MyStack(app, 'Stack', {
  env: {
    account: '123456',
    region: 'us-east-1',
  },
});
// Do
new MyStack(app, 'Stack', {
  env: {
    region: process.env.CDK_DEFAULT_REGION,
    account: process.env.CDK_DEFAULT_ACCOUNT,
  },
});

more details S004-Do-not-hardcode-env.ts

S005: Lambda layer

.
├── index.ts
└── lambda/
    ├── package-lock.json
    ├── package.json
    └── src/
        └── index.js*

2 directories, 4 files
const layer = new lambda.LayerVersion(this, 'MyLayer', {
  code: lambda.Code.fromAsset(path.join(__dirname, './lambda/'), {
    bundling: {
      image: lambda.Runtime.NODEJS_12_X.bundlingDockerImage,
      command: [
        'bash', '-xc', [
          'export npm_config_update_notifier=false',
          'export npm_config_cache=$(mktemp -d)', // https://github.com/aws/aws-cdk/issues/8707#issuecomment-757435414
          'cd $(mktemp -d)',
          'cp -v /asset-input/package*.json .',
          'npm i --only=prod',
          'mkdir -p /asset-output/nodejs/',
          'cp -au node_modules /asset-output/nodejs/',
        ].join('&&'),
      ],
    },
  }),
  compatibleRuntimes: [lambda.Runtime.NODEJS_12_X],
  description: 'A layer to test the L2 construct',
});

new lambda.Function(this, 'MyHandler', {
  runtime: lambda.Runtime.NODEJS_12_X,
  code: lambda.Code.fromAsset(path.join(__dirname, './lambda/src')),
  handler: 'index.handler',
  layers: [layer],
});

more details index.ts