Skip to content

Commit 7674df6

Browse files
committed
feat: allow inclusion of image copy to pipeline
1 parent fbeeee1 commit 7674df6

File tree

3 files changed

+911
-21
lines changed

3 files changed

+911
-21
lines changed

src/index.ts

+36-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import * as child_process from 'child_process';
66
import * as path from 'path';
77
import {
8+
aws_codepipeline as codepipeline,
9+
aws_codepipeline_actions as codepipeline_actions,
810
aws_ec2 as ec2,
911
aws_iam as iam,
1012
aws_lambda as lambda,
@@ -14,7 +16,6 @@ import {
1416
Token,
1517
DockerImage,
1618
} from 'aws-cdk-lib';
17-
import { PolicyStatement, AddToPrincipalPolicyResult } from 'aws-cdk-lib/aws-iam';
1819
import { Construct } from 'constructs';
1920
import { shouldUsePrebuiltLambda } from './config';
2021

@@ -77,6 +78,11 @@ export interface ECRDeploymentProps {
7778
* The environment variable to set
7879
*/
7980
readonly environment?: { [key: string]: string };
81+
82+
/**
83+
* Stage to include lambda to. If this is set, lambda is invoked in pipeline instead of custom resource.
84+
*/
85+
readonly stage?: codepipeline.IStage;
8086
}
8187

8288
export interface IImageName {
@@ -200,7 +206,9 @@ export class ECRDeployment extends Construct {
200206
constructor(scope: Construct, id: string, props: ECRDeploymentProps) {
201207
super(scope, id);
202208
const memoryLimit = props.memoryLimit ?? 512;
203-
this.handler = new lambda.SingletonFunction(this, 'CustomResourceHandler', {
209+
const lambdaId = props.stage ? 'ImageCopyHandler' : 'CustomResourceHandler';
210+
const lambdaPurpose = props.stage ? 'ImageCopy' : 'Custom::CDKECRDeployment';
211+
this.handler = new lambda.SingletonFunction(this, lambdaId, {
204212
uuid: this.renderSingletonUuid(memoryLimit),
205213
code: lambda.Code.fromAsset(path.join(__dirname, '../lambda'), {
206214
bundling: {
@@ -254,7 +262,7 @@ export class ECRDeployment extends Construct {
254262
runtime: lambda.Runtime.GO_1_X,
255263
handler: 'main',
256264
environment: props.environment,
257-
lambdaPurpose: 'Custom::CDKECRDeployment',
265+
lambdaPurpose,
258266
timeout: Duration.minutes(15),
259267
role: props.role,
260268
memorySize: memoryLimit,
@@ -298,19 +306,33 @@ export class ECRDeployment extends Construct {
298306
props.src.creds?.secretManager?.secret.grantRead(handlerRole);
299307
props.dest.creds?.secretManager?.secret.grantRead(handlerRole);
300308

301-
new CustomResource(this, 'CustomResource', {
302-
serviceToken: this.handler.functionArn,
303-
resourceType: 'Custom::CDKBucketDeployment',
304-
properties: {
305-
SrcImage: props.src.uri,
306-
SrcCreds: formatCredentials(props.src.creds),
307-
DestImage: props.dest.uri,
308-
DestCreds: formatCredentials(props.dest.creds),
309-
},
310-
});
309+
if (props.stage) {
310+
props.stage.addAction(
311+
new codepipeline_actions.LambdaInvokeAction({
312+
lambda: this.handler,
313+
actionName: lambdaPurpose,
314+
userParameters: {
315+
SrcImage: props.src.uri,
316+
SrcCreds: formatCredentials(props.src.creds),
317+
DestImage: props.dest.uri,
318+
DestCreds: formatCredentials(props.dest.creds),
319+
},
320+
}));
321+
} else {
322+
new CustomResource(this, 'CustomResource', {
323+
serviceToken: this.handler.functionArn,
324+
resourceType: lambdaPurpose,
325+
properties: {
326+
SrcImage: props.src.uri,
327+
SrcCreds: formatCredentials(props.src.creds),
328+
DestImage: props.dest.uri,
329+
DestCreds: formatCredentials(props.dest.creds),
330+
},
331+
});
332+
}
311333
}
312334

313-
public addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult {
335+
public addToPrincipalPolicy(statement: iam.PolicyStatement): iam.AddToPrincipalPolicyResult {
314336
const handlerRole = this.handler.role;
315337
if (!handlerRole) { throw new Error('lambda.SingletonFunction should have created a Role'); }
316338

0 commit comments

Comments
 (0)