Skip to content

Commit

Permalink
Replace CdkPipeline construct with vanilla Pipeline construct
Browse files Browse the repository at this point in the history
The former doesn't support lambdas with code in another repository: aws/aws-cdk#13600
  • Loading branch information
99heitor committed Mar 20, 2021
1 parent 4341a27 commit 1b9735c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
# CDK asset staging directory
.cdk.staging
cdk.out
dist
9 changes: 6 additions & 3 deletions bin/telegram-command-handler-cdk.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { TelegramCommandHandlerStack } from '../lib/stack/pipeline-stack';
import { PipelineStack } from '../lib/stack/pipeline-stack';
import { TelegramCommandHandlerStack } from '../lib/stack/telegram-handler-stack'

const app = new cdk.App();
new TelegramCommandHandlerStack(app, 'TelegramCommandHandlerStack', {
env: {region: 'us-west-2'}
const telegramCommandHandlerStack = new TelegramCommandHandlerStack(app, 'TelegramCommandHandlerStack');
new PipelineStack(app, 'PipelineStack', {
env: {region: 'us-west-2'},
pkmnQuizBotCode: telegramCommandHandlerStack.pkmnQuizBotCode
});

app.synth();
16 changes: 16 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 0.2

phases:
install:
commands:
npm install
build:
on-failure: ABORT
commands:
- npm run build
- npm run cdk synth -- -o dist

artifacts:
base-directory: dist
files:
- LambdaStack.template.json
122 changes: 72 additions & 50 deletions lib/stack/pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,88 @@ import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as lambda from '@aws-cdk/aws-lambda';
import { Construct, Stack, StackProps, Stage } from '@aws-cdk/core';
import { CdkPipeline, SimpleSynthAction } from "@aws-cdk/pipelines";

/**
* The stack that defines the application pipeline
*/
export class TelegramCommandHandlerStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
export interface PipelineStackProps extends StackProps {
readonly pkmnQuizBotCode: lambda.CfnParametersCode;
}

const cdkSourceArtifact = new codepipeline.Artifact();
const cloudAssemblyArtifact = new codepipeline.Artifact();
export class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props: PipelineStackProps) {
super(scope, id, props);

const cdkSourceAtifact = new codepipeline.Artifact('CdkSourceArtifact');
const cdkBuildArtifact = new codepipeline.Artifact('CdkBuildOutput');
const cdkRepo = codecommit.Repository.fromRepositoryName(this, 'CdkSourceRepo', "telegram-command-handler-cdk");


const pipeline = new CdkPipeline(this, 'Pipeline', {
pipelineName: 'TelegramCommandHandlerPipeline',
cloudAssemblyArtifact,

sourceAction: new codepipeline_actions.CodeCommitSourceAction({
actionName: 'CodeCommitCdkSourceUpdate',
output: cdkSourceArtifact,
repository: cdkRepo,
branch: 'main'
}),

synthAction: SimpleSynthAction.standardNpmSynth({
sourceArtifact: cdkSourceArtifact,
cloudAssemblyArtifact,

buildCommand: 'npm run build'
}),
const cdkBuild = new codebuild.PipelineProject(this, 'CdkBuild', {
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_2_0,
},
});

const pokemonQuizBotSourceArtifact = new codepipeline.Artifact();
const pokemonQuizBotBuiltArtifact = new codepipeline.Artifact();
const pokemonQuizBotRepo = codecommit.Repository.fromRepositoryName(this, 'PokemonQuizBotSourceRepo', "pokemon-quiz-bot");
const pokemonQuizBotBuild = new codebuild.PipelineProject(this, 'PokemonQuizBotBuild', {
const pkmnQuizBotSourceArtifact = new codepipeline.Artifact('PkmnSourceArtifact');
const pkmnQuizBotBuildArtifact = new codepipeline.Artifact('PkmnBuildArtifact');
const pkmnQuizBotRepo = codecommit.Repository.fromRepositoryName(this, 'PkmnQuizBotSourceRepo', "pokemon-quiz-bot");
const pkmnQuizBotBuild = new codebuild.PipelineProject(this, 'PkmnQuizBotBuild', {
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2
}
})

pipeline.stage('Source').addAction(
new codepipeline_actions.CodeCommitSourceAction({
actionName: "PkmnQuizBotSourceUpdate",
output: pokemonQuizBotSourceArtifact,
repository: pokemonQuizBotRepo,
branch: 'master'
})
)

const buildStage = pipeline.addStage('BuildCommandHandlerSource');
buildStage.addActions(
new codepipeline_actions.CodeBuildAction({
actionName: "PokemonQuizBotBuild",
input: pokemonQuizBotSourceArtifact,
project: pokemonQuizBotBuild,
outputs: [pokemonQuizBotBuiltArtifact]
}))

new codepipeline.Pipeline(this, 'Pipeline', {
stages: [
{
stageName: 'Source',
actions: [
new codepipeline_actions.CodeCommitSourceAction({
actionName: 'CodeCommit_Cdk_Source',
repository: cdkRepo,
output: pkmnQuizBotSourceArtifact,
branch: 'main'
}),
new codepipeline_actions.CodeCommitSourceAction({
actionName: 'CodeCommit_PkmnQuizBot_Source',
repository: pkmnQuizBotRepo,
output: cdkSourceAtifact,
branch: 'master'
})
],
},
{
stageName: 'Build',
actions: [
new codepipeline_actions.CodeBuildAction({
actionName: 'CDK_Build',
project: cdkBuild,
input: cdkSourceAtifact,
outputs: [cdkBuildArtifact],
}),
new codepipeline_actions.CodeBuildAction({
actionName: 'PkmnQuizBot_Build',
project: pkmnQuizBotBuild,
input: pkmnQuizBotSourceArtifact,
outputs: [pkmnQuizBotBuildArtifact],
}),
],
},
{
stageName: 'Deploy',
actions: [
new codepipeline_actions.CloudFormationCreateUpdateStackAction({
actionName: 'TelegramCommandHandler_CFN_Deploy',
templatePath: cdkBuildArtifact.atPath('TelegramCommandHandlerStack.template.json'),
stackName: 'TelegramCommandHandlerStack',
adminPermissions: true,
parameterOverrides: {
...props.pkmnQuizBotCode.assign(pkmnQuizBotBuildArtifact.s3Location),
},
extraInputs: [pkmnQuizBotBuildArtifact],
}),
],
},
],
});

}
}
32 changes: 13 additions & 19 deletions lib/stack/telegram-handler-stack.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import * as apigw from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';
import { CfnOutput, Construct, Stack, StackProps } from '@aws-cdk/core';
import * as path from 'path';
import { Construct, Stack, StackProps } from '@aws-cdk/core';

/**
* A stack for our simple Lambda-powered web service
*/
export class LambdaStack extends Stack {
/**
* The URL of the API Gateway endpoint, for use in the integ tests
*/
public readonly urlOutput: CfnOutput;


export class TelegramCommandHandlerStack extends Stack {
public readonly pkmnQuizBotCode: lambda.CfnParametersCode;

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

// The Lambda function that contains the functionality
const handler = new lambda.Function(this, 'Lambda', {
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'handler.handler',
code: lambda.Code.fromAsset(path.resolve(__dirname, 'lambda')),

this.pkmnQuizBotCode = lambda.Code.fromCfnParameters();

const pkmnQuizBotLambda = new lambda.Function(this, 'Lambda', {
runtime: lambda.Runtime.GO_1_X,
handler: 'main',
code: this.pkmnQuizBotCode,
});

}
}
7 changes: 3 additions & 4 deletions lib/stage/prod.ts → lib/stage/telegram-handler-stage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { CfnOutput, Construct, Stage, StageProps } from '@aws-cdk/core';
import { LambdaStack } from '../stack/telegram-handler-stack';
import { TelegramCommandHandlerStack } from '../stack/telegram-handler-stack';

/**
* Deployable unit of web service app
*/
export class ProdStage extends Stage {
public readonly urlOutput: CfnOutput;
export class TelegramCommandHandlerStage extends Stage {

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

const service = new LambdaStack(this, 'LambdaStack');
const service = new TelegramCommandHandlerStack(this, 'TelegramCommandHandlerStack');

}
}

0 comments on commit 1b9735c

Please # to comment.