Simon Verhoeven
Simon Verhoeven

Reputation: 427

AWS CDK set Stage stageName

I have an aws cdk project using typescript. I plan to set up a pipeline with 2 stages (dev and prod).

I have a cdk stage which contains a stack with a gateway and lamba construct inside

/* eslint-disable max-classes-per-file */
/* eslint-disable import/prefer-default-export */
import { Construct, Stage, StageProps } from '@aws-cdk/core';

import * as cdk from '@aws-cdk/core';
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as apigw from '@aws-cdk/aws-apigateway';
// import { CdkPipelinesStack } from './cdk-pipeline-2-stack';

class CdkPipelinesStack extends cdk.Stack {
  public readonly urlOutput: cdk.CfnOutput;

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

    const handler = new lambda.Function(this, 'lambda', {
      code: lambda.Code.fromAsset(path.resolve(__dirname, 'lambda')),
      handler: 'index.handler',
      runtime: lambda.Runtime.NODEJS_12_X,
      environment: {},
    });

    const gw = new apigw.LambdaRestApi(this, 'Gateway', {
      description: 'Endpoint for lambda',
      handler,
    });

    this.urlOutput = new cdk.CfnOutput(this, 'url', { value: gw.url });
  }
}

export class CdkPipelinesDemoStage extends Stage {
  public readonly urlOutput: cdk.CfnOutput;

  constructor(scope: Construct, id: string, props?: StageProps) {
    super(scope, id, props);

    const service = new CdkPipelinesStack(this, 'webservice', { env: {}, stackName: 'bla' });

    this.urlOutput = service.urlOutput;
  }
}

I want to deploy 2 stages (dev and prod) in a pipeline as follows

 // Do this as many times as necessary with any account and region
    // Account and region may be different from the pipeline's.
    const devStage = pipeline.addApplicationStage(
      new CdkPipelinesDemoStage(this, 'Dev', {
        env: {
          account: '694710432912',
          region: 'ap-southeast-1',
        },
      })
    );

    devStage.addActions(
      new ManualApprovalAction({
        actionName: 'ManualApproval',
        runOrder: devStage.nextSequentialRunOrder(),
      })
    );

    // Do this as many times as necessary with any account and region
    // Account and region may be different from the pipeline's.
    pipeline.addApplicationStage(
      new CdkPipelinesDemoStage(this, 'Prod', {
        env: {
          account: '694710432912',
          region: 'ap-southeast-1',
        },
      })
    );

This works but both deployed gateways have stage name prod.

I want to set the stage name of my first stage to dev.

How can I do this?

I thought it could just be as simple as passing a stageName from StageProps to the stage constructor. But this causes a compiler error.

// Do this as many times as necessary with any account and region
    // Account and region may be different from the pipeline's.
    const devStage = pipeline.addApplicationStage(
      new CdkPipelinesDemoStage(this, 'Dev', {
        env: {
          account: '694710432912',
          region: 'ap-southeast-1',
        },
        stageName: 'dev'
      })
    );


Thanks :)

Upvotes: 4

Views: 3048

Answers (1)

Simon Verhoeven
Simon Verhoeven

Reputation: 427

I solved this using @aws-solutions-constructs/core

https://www.npmjs.com/package/@aws-solutions-constructs/core

import * as defaults from '@aws-solutions-constructs/core';

const apiGatewayProps = {
        restApiName: 'restApi',
        deployOptions: { stageName: "staging" },
      },

defaults.GlobalLambdaRestApi(this, lambdaFunction, apiGatewayProps);

Upvotes: 2

Related Questions