5
5
import * as child_process from 'child_process' ;
6
6
import * as path from 'path' ;
7
7
import {
8
+ aws_codepipeline as codepipeline ,
9
+ aws_codepipeline_actions as codepipeline_actions ,
8
10
aws_ec2 as ec2 ,
9
11
aws_iam as iam ,
10
12
aws_lambda as lambda ,
@@ -14,7 +16,6 @@ import {
14
16
Token ,
15
17
DockerImage ,
16
18
} from 'aws-cdk-lib' ;
17
- import { PolicyStatement , AddToPrincipalPolicyResult } from 'aws-cdk-lib/aws-iam' ;
18
19
import { Construct } from 'constructs' ;
19
20
import { shouldUsePrebuiltLambda } from './config' ;
20
21
@@ -77,6 +78,11 @@ export interface ECRDeploymentProps {
77
78
* The environment variable to set
78
79
*/
79
80
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 ;
80
86
}
81
87
82
88
export interface IImageName {
@@ -200,7 +206,9 @@ export class ECRDeployment extends Construct {
200
206
constructor ( scope : Construct , id : string , props : ECRDeploymentProps ) {
201
207
super ( scope , id ) ;
202
208
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 , {
204
212
uuid : this . renderSingletonUuid ( memoryLimit ) ,
205
213
code : lambda . Code . fromAsset ( path . join ( __dirname , '../lambda' ) , {
206
214
bundling : {
@@ -254,7 +262,7 @@ export class ECRDeployment extends Construct {
254
262
runtime : lambda . Runtime . GO_1_X ,
255
263
handler : 'main' ,
256
264
environment : props . environment ,
257
- lambdaPurpose : 'Custom::CDKECRDeployment' ,
265
+ lambdaPurpose,
258
266
timeout : Duration . minutes ( 15 ) ,
259
267
role : props . role ,
260
268
memorySize : memoryLimit ,
@@ -298,19 +306,33 @@ export class ECRDeployment extends Construct {
298
306
props . src . creds ?. secretManager ?. secret . grantRead ( handlerRole ) ;
299
307
props . dest . creds ?. secretManager ?. secret . grantRead ( handlerRole ) ;
300
308
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
+ }
311
333
}
312
334
313
- public addToPrincipalPolicy ( statement : PolicyStatement ) : AddToPrincipalPolicyResult {
335
+ public addToPrincipalPolicy ( statement : iam . PolicyStatement ) : iam . AddToPrincipalPolicyResult {
314
336
const handlerRole = this . handler . role ;
315
337
if ( ! handlerRole ) { throw new Error ( 'lambda.SingletonFunction should have created a Role' ) ; }
316
338
0 commit comments