Skip to content

Removing array items leaves nulls in cf json file #44

Open

Description

@akzincsystems

Given I have a function definition:

functions:
  DataEnricher:
    iamRoleStatements:
      - Effect: Allow
        Action:
          - apigateway:GET
        Resource:
          - !Sub 'arn:aws:apigateway:${AWS::Region}::/foobar*'
      - Effect: Allow
        Action:
          - ssm:GetParameter
          - ssm:GetParametersByPath
        Resource:
          - Fn::Sub: 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/foo/bar'
          - Fn::Sub: 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/foo/bar*'
      - Effect: Allow
        Action:
          - lambda:InvokeFunction
        Resource: ${cf:my-service.MyServiceLambdaArn, ''}

and I have an serverlessIfElse recipe:

custom:
  env: ${env:SLS_ENV, 'development'}
  serverlessIfElse:
    - If: '"${self:custom.env}" == "staging"'
      Exclude:
        - functions.DataEnricher.iamRoleStatements.1
        - functions.DataEnricher.iamRoleStatements.2

The plugin will remove the 2 IAM statements if custom.env == 'staging' BUT leaves null in their place: from cloudformation-template-update-stack.json, irrelevant parts omitted:

[
    "DataEnricherIamRoleLambdaExecution": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Policies": [
          {
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "apigateway:GET"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "arn:aws:apigateway:${AWS::Region}::/foobar*"
                    }
                  ]
                },
                null,
                null
              ]
            }
          }

]

This breaks deployments with error:

The CloudFormation template is invalid: [/Resources/DataEnricherIamRoleLambdaExecution/Type/Policies/0/PolicyDocument/Statement/3] 'null' values are not allowed in templates

Activity

  1. akzincsystems commented on Mar 26, 2024

    @akzincsystems
    Author

    There is already a PR to fix this: 42. It's been sitting around for some time. Can it be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions