serverless: using disableLogs option with a single httpApi fails to provision IamS Role
When using the ‘disableLogs’ option with a function set up to use httpApi rather than the default ‘restApi’ (example configuration below), the resulting IAMS role is not created properly. The configuration is generated without any ‘Resources’.
A workaround is to define a second ‘fake’ function which does not have ‘disableLogs’ option. This allows the role to be correctly generated.
The bug was likely introduced here: https://github.com/serverless/serverless/pull/8561/files
As you can see by looking at the code, it sort of relies on at least one defined function being able to get past the ‘return’ here: https://github.com/serverless/serverless/pull/8561/files#diff-634c192175fa19e078211273e8cec92851e3a2fb117fdf447b6998fa1af1a5cfR104
So if at least 1 function is not set up to ‘log’ then the policy is attempted to be created with an invalid ‘Resource []’ node (it should never be empty)
e.g
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup"
],
"Resource": []
},
{
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": []
}
]
}
}
I guess the next step would be to convert my provided serverless.yml into a failing test case?
serverless.yml
service: my-service
unresolvedVariablesNotificationMode: error
variablesResolutionMode: 20210219
provider:
name: aws
runtime: nodejs12.x
region: ${opt:region}
deploymentBucket:
name: ssr-lambda-${opt:region}
stage: ${opt:stage, self:custom.defaultStage}
profile: ${self:custom.profiles.${self:provider.stage}}
memorySize: 1024
logRetentionInDays: 5
lambdaHashingVersion: 20201221
endpointType: REGIONAL
apiGateway:
shouldStartNameWithService: true
package:
defaultStage: dev
profiles:
dev: devProfile
functions:
ssr:
handler: ./dist/index.handler
disableLogs: true
events:
- httpApi: '*'
NODE_ENV=production webpack && sls deploy output
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service our-code.zip file to S3 (13.22 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
....
Serverless: Operation failed!
Serverless: View the full error output: snipped
Serverless Error ----------------------------------------
An error occurred: IamRoleLambdaExecution - Policy statement must contain resources. (Service: AmazonIdentityManagement; Status Code: 400; Error Code: MalformedPolicyDocument; Request ID: 29809a22-xxx-4988-a82d-xxxxxxx; Proxy: null).
Installed version
> NODE_ENV=production sls --version
Framework Core: 2.29.0 (local)
Plugin: 4.5.0
SDK: 4.2.0
Components: 3.7.3
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 4
- Comments: 15 (2 by maintainers)
Commits related to this issue
- fix(aws-package): removes invalid policy document statements that have no resources Addresses #9107 — committed to lewgordon/serverless by deleted user 6 years ago
- fix(aws-package): removes invalid policy document statements that have no resources Addresses #9107 — committed to lewgordon/serverless by deleted user 6 years ago
- fix(aws-package): removes invalid policy document statements that have no resources Addresses #9107 — committed to lewgordon/serverless by deleted user 6 years ago
- fix(aws-package): removes invalid policy document statements that have no resources Addresses #9107 — committed to lewgordon/serverless by deleted user 6 years ago
- fix(aws-package): removes invalid policy document statements that have no resources Addresses #9107 — committed to lewgordon/serverless by deleted user 6 years ago
So far we have decided to disableLogs on everything but one function that doesn’t emit many in order to stop paying for duplicate logging costs between CloudWatch and a third party we emit logs to directly from Lambda.
I’m really disappointed this feature has been broken for years and no hope in sight for a fix.
The issue that apparently solved this problem was closed almost one year ago but I’m still facing this error as well.
serverless.ts
Temporary solution
I’m not proud of it but I managed to make a workaround for this issue.
As the CloudFormation template is generated with wrong “Resource” values and it’s a JSON, I created a script that runs between
sls packageandsls deployactions modifying the generated JSON CF Template removing those invalid resoures.To do this:
serverless.ts:.serverless/serverless-state.jsonfile, used bysls deploy.fix_disableLogs_resource.js
This is not perfect, but works and solved my problem.
Hi there, any news on this? We’re also getting this error while using a simple handler for SQS events:
Hi, Can someone guide me on disableLogs issue. I’m still facing it.
Hello @calumbrodie, thanks for reporting. I’ve managed to reproduce your issue locally. I believe in such situations we should not add the policy with
logs:CreateLogStream,logs:CreateLogGroupandlogs:PutLogEventsactions at all, it should ensure that the role can be created without issues.We’d be happy to accept a PR that fixes that problem 👍
This is a really interesting issue, that I’ve been seeing since at least one year (serverless 3.21.0). Today I decided to spend a little more time on it. My case is the following: deploy a function to AWS with
disableLogs: truefails. I’m not willing to go the custom script route, and looking at the closed PR discussion, I don’t think I can fix this myself. Here’s what I tried:disableLogs: falseworksiamRoleStatementsrule with effect Allow or Deny doesn’t worknameattribute doesn’t workSo for now, I will have to always keep one of the functions with logs, unfortunately.
Any update on this issue ?