How to create a custom resource from an existing lambda function
I am trying to create a custom resource in my CDK stack from an already deployed lambda function.
In normal CloudFormation I do this by creating a lambda function in one stack and then exporting the Arn of the lambda function. This can then be imported into other stacks.
In separate templates we import the value of the Arn and is it to set the ServiceToken of the Custom Resource using the Arn.
Trying to do the same in CDK is proving an issue.....
I tried doing the same thing for a CustomResource however the ServiceToken is not a property of the constructor of the custom resource.
There is a ServiceToken property which I assume is to provide the service token back to interested callers but no way that I can see of creating a custom resource from an existing service token.
Surely CDK does not expect you to create a new lambda function when there is already one available that I can use?
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudformation.CustomResourceProvider.html
Please advise how can I instantiate a custom resource from an existing lambda function arn?
Environment
CDK CLI Version: 1.15.0 (build bdbe3aa) OS: Ubuntu WSL Ubuntu 18.04 Language: TypeScript
You can do:
const existingLambda = lambda.Function.fromFunctionArn(this, 'Lambda', '<your arn>');
new cloudformation.CustomResource(this, 'Resource', {
provider: cloudformation.CustomResourceProvider.lambda(existingLambda),
properties: {
...
}
});
(if your other stack is also created with CDK you can simply pass the lambda object between the stacks)
Thanks, I didn't spot that constructor for the lambda - I wonder why they chose to create this link rather than having a property on the custom resource.
Closing this issue since a solution has been found. Feel free to open a feature request if you think the implementation could be improved.
I have been trying this thing for 5 days. I have a custom resource in AWS YAML format, which I want to create in AWS CDK. I am able to add condition and ServiceToken using AWS CDK CfnCustomResource but I can't add properties.
YAML Template
LookupAmi:
Condition: UseGI
Properties:
ServiceToken:
Fn::ImportValue: !Join ['', [!If [ MyProd, '', 'qa-'], Prod-LookupAmiFunction]]
AMI: {Ref: AMI}
appId: {Ref: AppId}
envType: {Ref: EnvType}
osType: {Ref: OSType}
Type: Custom::AmiLookup
Corresponding AWS CDK typescript code which is working.
const LookupAmi = new cfn.CfnCustomResource(this, 'LookupAmi', {
serviceToken : "DSDS" # Just a random value but it is working
});LookupAmi.cfnOptions.condition = UseGI
I want to add properties of YAML template, how could I do it.
This is how I create a custom resource that uses a lambda function (also created by CDK) for setting a bucket policy. The properties that are passed to the custom resources are a JSON object.
Hope this help!
const bucketPolicyUpdateCustomResource = new cfn.CustomResource(this, 'bucketPolicyUpdateCustomResource', {
provider: cfn.CustomResourceProvider.lambda(bucketPolicyUpdateFunction),
properties: {
"Bucket": sourceBucket.bucketName,
"Statement": {
"Sid": "BucketNotificationConfigurationPermission",
"Effect": "Allow",
"Principal": {
"AWS": bucketNotificationConfigurationFunction.role.roleArn
},
"Action": [
"s3:PutBucketNotification",
"s3:GetBucketNotification"
],
"Resource": sourceBucket.bucketArn
}
}
})
Hey @reidca, thanks for the reply, i really appreciate it. By the way that I don't want to use Lambda function or SNS topic (which are supported by cfn.CustomResource) instead I just want to have serviceToken of a service as a property and other custom properties as well, which will define my custom resource
i got the solution for this.
const MyAmi= new cdk.CfnResource(this, 'MyAmi', {
type : "Custom::MyAmi",
properties : {
ServiceToken : cdk.Fn.importValue(cdk.Fn.join('',[cdk.Fn.conditionIf('MyProd','','qa-').toString(),'Prod-LookupAmiFunction'])),
bizUnit: BizUnit,
AMI: AMI,
appId: AppId,
envType: EnvType
});LookupAmi.cfnOptions.condition = UsemE
@jogold I am look the for the approach for a whole day, Just curious how do you know we can do lambda.Function.fromFunctionArn(this, 'Lambda', '
@XiaolongLiuAtAutodesk https://docs.aws.amazon.com/cdk/api/latest/typescript/api/aws-lambda/function.html#aws_lambda_Function_fromFunctionArn