Skip to content

Unnecessary CodeDeployServiceRole created when template contains at least one enabled and one disabled DeploymentPreference #1496

@robertsigler

Description

@robertsigler

Description:

I want to create a SAM template which uses a DeploymentPreference to specify how it should be deployed. However, I do not want SAM to create a CodeDeploy service role on my behalf--I want to provide a custom one. When I have at least one enabled DeploymentPreference, and at least one disabled DeploymentPreference in my template, the default CodeDeployServiceRole gets created in the 'translated' template, even if you provide your custom Role for every DeploymentPreference.

It looks like this is happening because the role field on the DeploymentPreference object is None for a disabled deployment preference, even when a role is provided. The disabled deployment preference is included in the list of deployment preferences that are considered in can_skip_service_role() (https://github.com/awslabs/serverless-application-model/blob/develop/samtranslator/model/preferences/deployment_preference_collection.py#L73) even though maybe it shouldn't be.

Steps to reproduce the issue:

  1. Run bin/sam-translate.py on the following template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Template with unnecessary service role

Resources:

  Function:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lambda.lambda_handler
      Role: arn:aws:iam::123456789999:role/lambda-role
      Runtime: python3.7
      CodeUri: s3://bucket/key
      AutoPublishAlias: live
      DeploymentPreference:
        Type: Linear10PercentEvery1Minute
        Role: arn:aws:iam::123456789999:role/custom-codedeploy-servicerole
        Hooks:
          PreTraffic: !Ref preTrafficHook
        Events:
          Api:
            Type: Api
            Properties:
              Path: /test
              Method: get

  preTrafficHook:
    Type: AWS::Serverless::Function
    Properties:
      Handler: hook.lambda_handler
      Role: arn:aws:iam::123456789999:role/lambda-role
      Runtime: python3.7
      CodeUri: s3://bucket/key
      FunctionName: 'CodeDeployHook_preTrafficHook'
      AutoPublishAlias: live
      DeploymentPreference:
        Enabled: false
        Role: arn:aws:iam::123456789999:role/custom-codedeploy-servicerole
        Type: Linear10PercentEvery1Minute
      Timeout: 5
      Environment:
        Variables:
          NewVersion: !Ref Function.Version

Observed result:
The output template contains a resource called "CodeDeployServiceRole" which is not used anywhere

Expected result:'
The output template does not contain a resource called "CodeDeployServiceRole"

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions