# Custom Resources Lambda using deploymentRole?

**URL:** <https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039>\
**Category:** Serverless Framework\
**Created:** [October 7, 2021, 7:54am UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039 "2021-10-07T07:54:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_vandenbuys](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@steve\_vandenbuys](https://forum.serverless.com/u/steve_vandenbuys)\
**Post date:** [October 7, 2021, 7:54am UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/1 "2021-10-07T07:54:08Z")

</div>

Hello,

I’m running into an issue: I want to control the rights given to CloudFormation when it comes to deploying a Serverless API. So, I create a role with just the required permissions and add this to the serverless.yml:

> iam:  
> deploymentRole: arn:aws:iam::${aws:accountId}:role/cloudformation-serverless-execution-role

When I run this, execution fails with the following message:

> An error occurred: CustomDashresourceDashapigwDashcwDashroleLambdaFunction - Resource handler returned message: "The role defined for the function cannot be assumed by Lambda.

This confused me for a while as I failed to understand how a role applied to CloudFormation could give me that error. Things got even more confusing when I simply removed the deploymentRole from serverless.yml after which everything worked perfectly (with the role still being used by CloudFormation as the stack already existed and so the IAM role remained in place).

I eventually looked into the serverless-state.json where I found this:

> “CustomDashresourceDashapigwDashcwDashroleLambdaFunction”: {  
> “Type”: “AWS::Lambda::Function”,  
> “Properties”: {  
> “Code”: {  
> “S3Bucket”: {  
> “Ref”: “ServerlessDeploymentBucket”  
> },  
> “S3Key”: “serverless/demo-api/dev/1633590891473-2021-10-07T07:14:51.473Z/custom-resources.zip”  
> },  
> “FunctionName”: “demo-api-dev-custom-resource-apigw-cw-role”,  
> “Handler”: “apiGatewayCloudWatchRole/handler.handler”,  
> “MemorySize”: 1024,  
> “Runtime”: “nodejs12.x”,  
> “Timeout”: 180,  
> “Role”: “arn:aws:iam::XXXXXXXXXXXX:role/cloudformation-serverless-execution-role”  
> },

Why is the deploymentRole being applied to a Lambda function while the Serverless.yml reference page clearly states that this role is used for CloudFormation?

Seems like a bug to me because when not using the deploymentRole, a custom resources Lambda execution role gets generated that, as expected, has a very different policy than the policy I created for CloudFormation.

---

<div class="post-metadata">

**Author:** ![seb1](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/seb1/32/5427_2.png) [@seb1](https://forum.serverless.com/u/seb1)\
**Post date:** [October 20, 2021, 7:09am UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/2 "2021-10-20T07:09:34Z")

</div>

Just a guess, but the deployerRole is probably assumed by Cloudfront to deploy the actual resources, only after the Cloudformation template has been synthesised using the role/profile that the serverless cli is running under.

---

<div class="post-metadata">

**Author:** ![steve\_vandenbuys](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@steve\_vandenbuys](https://forum.serverless.com/u/steve_vandenbuys)\
**Post date:** [October 20, 2021, 7:34am UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/3 "2021-10-20T07:34:57Z")

</div>

The deploymentRole is indeed assumed by CloudFront to deploy the actual resources. That is also what I want so no problem there.

The problem is: why is a role created for CloudFront being applied to a Lambda function (that as I understand it is being auto-generated for the Dashboard functionality which I don’t even use)?

If deploymentRole is present, it should only be applied to CloudFront and not to some auto-generated Lambda. When not using deploymentRole another role is generated for that Lambda which is also what should continue to happen when deploymentRole is being used as that auto-generated role works just fine.

---

<div class="post-metadata">

**Author:** ![mkh](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mkh](https://forum.serverless.com/u/mkh)\
**Post date:** [March 17, 2022, 5:50pm UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/4 "2022-03-17T17:50:44Z")

</div>

Hi were you able to find a resolution to this issue? Currently running into the exact same issue and not sure what to do.

---

<div class="post-metadata">

**Author:** ![penseleit](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/penseleit/32/7046_2.png) [@penseleit](https://forum.serverless.com/u/penseleit)\
**Post date:** [September 7, 2022, 7:33am UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/5 "2022-09-07T07:33:15Z")

</div>

I was able to fix this issue for my setup.

Find out what role your CustomDashresourceDashapigwDashcwDashroleLambdaFunction uses with provider.iam.deploymentRole turned off/disabled.

Copy that role info from serverless-state.json and convert it to YAML with [https://json2yaml.com/](https://json2yaml.com/)

Add the new role to your resources section in serverless.yml.

Example:

IamRoleCustomResourcesLambdaExecution:  
Type: AWS::IAM::Role  
Properties:  
AssumeRolePolicyDocument:  
Version: ‘2012-10-17’  
Statement:  
- Effect: Allow  
Principal:  
Service:  
- [lambda.amazonaws.com](http://lambda.amazonaws.com)  
Action:  
- sts:AssumeRole  
Policies:  
- PolicyName:  
Fn::Join:  
- “-”  
- - ${self:custom.stage}  
- myproject  
- custom-resources-lambda  
PolicyDocument:  
Version: ‘2012-10-17’  
Statement:  
- Effect: Allow  
Resource:  
…

Then add an extensions section to the end of your resources section to overwrite the deploymentRole on the CustomDashresource… resources.

extensions:  
CustomDashresourceDashapigwDashcwDashroleLambdaFunction:  
Properties:  
Role:  
Fn::GetAtt:  
- IamRoleCustomResourcesLambdaExecution  
- Arn

Re-enable provider.iam.deploymentRole and sls deploy…

Worked for me.

---

<div class="post-metadata">

**Author:** ![BeckhamKyrie](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@BeckhamKyrie](https://forum.serverless.com/u/BeckhamKyrie)\
**Post date:** [November 8, 2022, 4:20pm UTC](https://forum.serverless.com/t/custom-resources-lambda-using-deploymentrole/16039/6 "2022-11-08T16:20:46Z")

</div>

**To use a custom resource, you’ll need to do three things:**

1. Write the logic for your custom resource;
2. Make your custom resource login available by deploying to an AWS Lambda function or by subscribing to an SNS topic[.](https://knifeplatoon.com/how-to-fillet-a-perch/)
3. Use the custom resource in your Formation template that references the Lambda function or SNS topic
