Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
robertsigler opened this issue Mar 6, 2020 · 11 comments
Assignees
Labels

Comments

@robertsigler
Copy link

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"

@ShreyaGangishetty
Copy link

Thank for reporting this issue! I agree that for Enabled: False SAM should not create CodeDeployServiceRole.
SAM adds this role here. This role needs to be added after the Enabled property is set for this resource

@Lukas-Franz
Copy link

We've got the same issue. We're passing a role to DeploymentPreference and SAM tries to create a CodeDeployServiceRole which isn't even used after the transformation.

@robsigler
Copy link
Contributor

The fix for this is simple--it's only being busy which has kept me from fixing it for so long. Here's a pull request that solves the problem: #1662

@qingchm qingchm self-assigned this Nov 13, 2020
@martnr
Copy link

martnr commented Aug 5, 2021

We have the same issue even if the all DeploymentPreference have Enabled: true.

Is there a tentative date when the fix will be available?

@martnr
Copy link

martnr commented Aug 5, 2021

What if you don't have any role assigned to the DeploymentPreference and it is Enabled: True? will the 9/25 update make DeploymentPreference not require a role?

@mildaniel
Copy link
Contributor

@hawflau has merged a fix. These changes will be pushed out in one of the coming releases.

@michael-k
Copy link

Would have been nice to see this in the changelog. Now that I'm here, I remember seeing this issue, but when the stack wanted to remove the role, we were a bit confused what's happening.

@moelasmar
Copy link
Contributor

The fix got released, and I was able to verify that the CodeDeployServiceRole is not generated.

@james-bellamy
Copy link

Hello, I'm currently experiencing the warning about this issue but all the DeploymentPreferences in the template.yaml don't have Enabled: false, or any Enabled: at all.
I tried adding Enabled: true and a Role to all preferences, and I added DeploymentPreference to all functions which didn't have it.
None of these removed the warning, is there something else I can do?
Currently building on build-python3.7:latest

@moelasmar
Copy link
Contributor

@james-bellamy could you please elaborate more about this warning. Can you give me the steps to reproduce this issue.

@wpark95
Copy link

wpark95 commented Feb 21, 2023

Hi, could someone enlighten me on what is the expected behavior when all DeploymentPreference has Enabled: true but no roles are defined in the CloudFormation template? Similar to question from @martnr. I noticed that my CloudFormation stack creates CodeDeployServiceRole but ends up deleting the CodeDeployServiceRole right before stack update is complete, even when DeploymentPreference has Enabled: true. Is this expected with this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests