IAM Policies should be created before Lambda function
This is a Bug Report
Description
- What went wrong? I'm creating a new service which will need access to VPC resources. For this to work, my lambda function need to have additional permissions at the time they are created: "ec2:CreateNetworkInterface", "ec2:DescribeNetworkInterfaces" and "ec2:DeleteNetworkInterface" I have added those statements in the "iamRoleStatements" provider property in my serverless.yml file, and I can confirm that they get added to the CloudFormation template correctly, but once the IAM role is created, the IAM policy adding those permissions and the lambda functions are created at the same time by Cloudformation, meaning that the Lambda functions don't have the extra permissions yet, and fail to deploy (see screenshot to prove the policy and lambda functions were created at the same time).
- What should have happened? The lambda functions need to have a dependency specified for the IAM policy creation so that they can be deployed successfully in the case of a VPC deployment.
- Configuration used:
provider:
name: aws
runtime: nodejs4.3
iamRoleStatements:
- Effect: "Allow"
Action:
- "ec2:CreateNetworkInterface"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DeleteNetworkInterface"
Resource: "*"
vpc:
securityGroupIds:
- Ref: LambdaSecurityGroup
subnetIds: ${SubnetIds}
- Error message from Cloudformation: "Your access has been denied by EC2, please make sure your function execution role have permission to CreateNetworkInterface. EC2 Error Code: UnauthorizedOperation. EC2 Error Message: You are not authorized to perform this operation."
- Workaround: For a new deployment to a new stage/region, if I comment out the "vpc" provider configuration section, deploy, uncomment the section and deploy again this allows the IAM policy to be created first and the lambda deployment to still succeed.
Additional Data
- Using Serverless 1.0 beta2, on Windows 10
- Error screenshot:

I just tried adding the "DependsOn" property to serverless/lib/plugins/aws/deploy/compile/functions/index.js And while it now creates the policy before the lambda functions, I still get the same errors. I'll keep looking
@fruffin I am working with AWS on this currently. There is a PR around this topic already. https://github.com/serverless/serverless/pull/1934.
My current patch for serverless is https://github.com/serverless/serverless/compare/master...andymac4182:Auto_Create_VPC_IAM_Policy?expand=1 and this is working 99% of the time. This adds an arbitrary LogGroup as a dependency between the Policy and Functions. It enables there to be enough lag for the policy to take effect.
@andymac4182 just to make sure this is an AWS bug in general that we can do a Workaround for, but we can't really fix correct?
Hi @flomotlik
It's not only a bug in AWS. The problem in serverless is that there is no dependency between the IAM policy and the lambda functions, so they end up being created at the same time by CloudFormation. This can easily be fixed by adding the "DependsOn" property in the function template as mentioned in my previous comment. Once this is done there is a bug in AWS whereby it looks like when CloudFormation gets the completion signal for the IAM policy it is not yet active on the IAM role used by the Lambda function. This is causing the lambda function creation to fail in the exact same way as above. @andymac4182 pointed out that this can be fixed by introducing a short delay between the IAM policy creation and the lambda function creation.
We both have a similar workaround for this in the comments on #1934 My option was to add a "DependsOn" property to my custom resource creating the security group for the lambda VPC integration, to depend on the IAM policy. So this way, Cloudformation now creates the IAM role, then the IAM policy, then the security group and finally the lambda functions. The time it takes for the security group to be created is enough for the IAM policy to become active on the IAM role.
Does that make sense?
I got a answer from AWS after some back and forward. They use the word almost immediately a few times in their faqs which from what people are saying online can be up to 5-10 seconds. This to me says that a solution with a intermediate resource will be a permanent solution to this. I like the idea of being able to specify the security group definition at either a provider level or being able to specify it as a custom resource and then referencing from the functions that I want to use that security group. This is a good default since anyone using VPCs should understand what they want to open up.
Regarding your query in the previous correspondence, I would say it a known lag and you can read up on the links below to understand more.
http://stackoverflow.com/questions/20156043/how-long-should-i-wait-after-applying-an-aws-iam-policy-before-it-is-valid
https://aws.amazon.com/iam/faqs/
The suggested workaround is to embed the policy to the role, so when the role is CREATE_COMPLETE, it is assured that the policy resource has also been successfully created.
This issue will be fixed with https://github.com/serverless/serverless/issues/1786. Closing.
Found what seems to be a reliable workaround for the moment, I have simply declared a dummy log group in the serverless.yml that DependsOn: IamPolicyLambdaExecution, this seems to ensure that the policy is fully in place before the function is created.
DummyLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: IamPolicyLambdaExecution
Properties:
LogGroupName: dummy
RetentionInDays: 1