aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

VpcEndpointIds getting removed from private APIs post stack update. Stack shows drifted.

Open saurabhshri opened this issue 6 years ago • 27 comments

Description:

I have a private API that is being provisioned using SAM. Here's how it looks

PrivateApiResource:
    Type: AWS::Serverless::Api
    Tags:
      Application: !Ref Application
    Properties:
      Name: !Sub ${Env}-${Application}-PrivateApiResource
      StageName: prod
      EndpointConfiguration:
        Type: PRIVATE
        VPCEndpointIds:
          - !FindInMap [ EnvConfig, !Ref Env, VPCEndpointAPI ]
      Auth:
        ResourcePolicy:
          CustomStatements:
            - Effect: Allow
              Principal: '*'
              Action: execute-api:Invoke
              Resource: "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*/*/*"
            - Effect: Deny
              Principal: '*'
              Action: execute-api:Invoke
              Resource: "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*/*/*"
              Condition:
                StringNotEquals:
                  aws:SourceVpce: !FindInMap [ EnvConfig, !Ref Env, VPCEndpointAPI ]

I have a lambda that has the above API as event resource :

SayHelloPrivately:
    Type: AWS::Serverless::Function
    Properties:
      Handler: ./target/index.sayHelloPrivatelyHandler
      Runtime: nodejs12.x
      CodeUri:
        Bucket: !Ref Bucket
        Key: !Sub ${Application}/latest-lambda.zip
      Events:
        SayHelloApi:
          Type: Api
          Properties:
            Path: /hello
            Method: GET
            RestApiId: !Ref PrivateApiResource

This worked and I was able to access /hello endpoint when on VPC and it times out when not in VPC.

Now anytime stack updates and there's a related update to the API resource, like adding a new lambda with different endpoint, the VPCEndpointIds and Tags seems to be getting removed randomly. I am not exactly sure what exact event triggers it. But most recently, doing as little adding a third lambda lead to removal of both tags and VPCe.

CloudFormation stack's template tab shows that the template still has VPCe. Stack shows it's drifted and both are removed.

  • template, as it appears in CloudFormation stack tab
Screenshot 2020-11-05 at 12 28 52 PM
  • Stack drift :
Screenshot 2020-11-05 at 12 30 07 PM
  • VPCe removed
Screenshot 2020-11-05 at 12 28 18 PM

Nothing related to VPCEndpointIds/Tags were done. Even the API resource was untouched in stack update. Only a new lambda was created which added a new endpoint to the API resource, similar to how it's done in SayHelloPrivately above.

Steps to reproduce:

Follow the steps in description.

Observed result:

VPCEndpointIds from private API and tags are removed. Stack shows drifted.

Expected result:

Nothing should have changed with this aspect.

NOTE : This has happened more than once.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Amazon Linux 2
  2. sam --version: 1.1.0

Almost forgot to tag @awsjeffg ! Let me know if I can provide any other information. Thanks!

saurabhshri avatar Nov 05 '20 07:11 saurabhshri

If it helps, I tried looking through CloudTrail to see if I can spot something.

Screenshot 2020-11-05 at 4 52 42 PM
  1. When it was working, I can see in event UpdateRestApi November 03, 2020, 19:21:30 (UTC+05:30) (the bottom most entry) the following :
"requestParameters": {
        "restApiId": "xxxxxxxx",
        "updateRestApiInput": {
            "patchOperations": [
                {
                    "path": "/endpointConfiguration/types/EDGE",
                    "value": "PRIVATE",
                    "op": "replace"
                },
                {
                    "path": "/name",
                    "value": "dev-xxxxx-PrivateApiResource",
                    "op": "replace"
                },
                {
                    "path": "/endpointConfiguration/vpcEndpointIds",
                    "value": "vpce-xxxxxx",
                    "op": "add"
                }
            ]
        },
        "template": false
    },

and

        "endpointConfiguration": {
            "vpcEndpointIds": [
                "vpce-xxxxx"
            ],
            "types": [
                "PRIVATE"
            ],
            "ipv6": false
        },

Later when another lambda was added, the new event UpdateRestApi (November 03, 2020, 21:24:42 (UTC+05:30) (third from top) has the following :

"requestParameters": {
        "restApiId": "xxxxxx",
        "updateRestApiInput": {
            "patchOperations": [
                {
                    "path": "/name",
                    "value": "dev-xxxx-PrivateApiResource",
                    "op": "replace"
                }
            ]
        },
        "template": false
    },

and

        "endpointConfiguration": {
            "types": [
                "PRIVATE"
            ],
            "ipv6": false
        },

saurabhshri avatar Nov 05 '20 11:11 saurabhshri

Thanks for providing the details. I'll try to reproduce it

hawflau avatar Nov 05 '20 19:11 hawflau

Thank you @hawflau & @awsjeffg. Let me know how can I help.

saurabhshri avatar Nov 06 '20 10:11 saurabhshri

Hey @saurabhshri, I couldn't reproduce the result for the Tags property. It seems you placed in at the wrong place in your example. It should be placed under Properties instead. Also the tags will be created under the AWS::ApiGateway::Stage resource.

hawflau avatar Nov 06 '20 17:11 hawflau

@hawflau you're absolutely right, Tags are indeed not at correct place. The only reason I pointed out tags was I saw it in Drift while I was checking vanished VPCEndpointIds (hence the title only mentioned VPCe). I never verified Tags from the console. Sorry if it threw you off. Any luck with VPCe?

saurabhshri avatar Nov 06 '20 17:11 saurabhshri

However, I managed to reproduce the result on VPCEndpointIds getting removed. The problem doesn't seem to be related to SAM or SAM CLI though - the property was not gone after SAM translation.

So I did another experiment - I created a pure CloudFormation template with a AWS::ApiGateway::RestApi resource with VPCEndpointIds and swagger defined; created a stack using this template; updated swagger in the template; created changeset and executed it. And VPCEndpointIds was gone! Note that in the changeset, there was not any mentioning about the removal of the property.

hawflau avatar Nov 06 '20 17:11 hawflau

However, I managed to reproduce the result on VPCEndpointIds getting removed.

Thank you @hawflau, for a moment there I thought I was going crazy. :)

The problem doesn't seem to be related to SAM or SAM CLI though - the property was not gone after SAM translation.

Correct, I assumed so too, because the translate CF template seemed to have VPCEndpointIds as expected..

So I did another experiment - I created a pure CloudFormation template with a AWS::ApiGateway::RestApi resource with VPCEndpointIds and swagger defined; created a stack using this template; updated swagger in the template; created changeset and executed it. And VPCEndpointIds was gone! Note that in the changeset, there was not any mentioning about the removal of the property.

Sounds like it matches more or less my experience. Not changing anything related to VPCe, but any modification to API be it adding another endpoint -- may lead to removal of the VPCe.

So, does that mean it's an issue with CF? Do you know what would be the next steps here.

saurabhshri avatar Nov 06 '20 18:11 saurabhshri

So, does that mean it's an issue with CF? Do you know what would be the next steps here.

@saurabhshri thanks again for reporting this. You are correct, we are for now considering this a possible Cloudformation issue and are creating an internal ticket with that team, however we will keep this issue open for the time being and update here as events warrant.

awsjeffg avatar Nov 06 '20 18:11 awsjeffg

Thank you for looking into this so quickly and raising it internally @awsjeffg @hawflau !

however we will keep this issue open for the time being and update here as events warrant.

That is really helpful, thank you. Let me know if I can be of any help.

saurabhshri avatar Nov 06 '20 19:11 saurabhshri

Ran into this issue today. Any ETA at this time?

srinadhv9 avatar Dec 04 '20 23:12 srinadhv9

Any update on this? or CF ref ticket?

  • I have seen that 1st CF deployment goes fine but next updates are the ones that remove the vpc endpoints
  • I have also 3 environments using the same template dev, stage and prod and the weirdest thing is that dev works fine but test and dev are faulty.
  • Could this be something related with the permissions of the role executing CF?

camiloazula avatar Jan 12 '21 12:01 camiloazula

An update for folks facing the same issue : we opened a support ticket since this was restricting us in moving forward with private api gateways in production. We got a response acknowledging that this is a known behaviour and that there is already an internal ticket on it which the service team is actively working on.

... this is a known behavior which occurs when the stack is updated. CloudFormation makes a PutRestApi [1] call in overwrite mode, and then makes an UpdateRestApiCall[2] without adding the VPC endpoint parameter in the request. Hence, this leads to disassociation of VPC endpoints from the API Gateway.

References:- [1] https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-rest-api.html [2] https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-rest-api.html

Unfortunately, they couldn't provide an ETA on the fix, but suggested a workaround to manually reattach the VPC Endpoint via the API Gateway settings page.

... This can be done using the following steps:

  1. Navigate to the API Gateway Console
  2. Select the API in question
  3. Navigate to "Settings"
  4. Under "Endpoint Configuration" enter the VPC Endpoint ID in the text box, and press the "Add" button.
  5. Scroll to the bottom of the Settings page and click the "Save Changes" button.

Hopefully it helpful to those who are trying to figure out the issue and fix from their end.

saurabhshri avatar Feb 09 '21 08:02 saurabhshri

This issue has been open since November and is production impacting? I am floored by the fact that aws would allow this to go on for so long. The behavior i have seen has been random in that some times the vpc endpoint gets removed and other times it does not. On top of that, after i make the manual change it sometimes takes a moment to go into effect. During this time, my private api gateway endpoint is unreachable and my production system is down.
I just got the same advice from support to use the manual workaround and i couldn't believe what i was reading. CloudFormation/SAM is an automation tool, why would we be instructed to followup an automation tool with a manual step? Crazy. This has been an issue for more than six months and i would hope that something production impacting would have a higher priority.
Please provide an update on when we can expect a fix.

jaglade avatar May 20 '21 16:05 jaglade

Hi @jaglade, apologies for the delay. The fix for the issue completed deployment earlier this month.

sapessi avatar Jun 18 '21 17:06 sapessi

Closing as the issue has been fixed. Feel free to reopen if there's still issue.

hawflau avatar Jun 21 '21 20:06 hawflau

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jun 21 '21 20:06 github-actions[bot]

@sapessi You mention this being fixed and it completing deployment earlier this month. AWS Support is saying that this is only fixed for stacks created after March 29, 2021 and that stacks created before then you have to manually specify 'Merge' as the Mode in the properties on the template. However 'Mode' does not exist in SAM as a property at the moment. Their solution from there was to manually add back in the endpoint ID after deploying via the console. However, this means that on production deployments all of our private API gateways will have downtime between the deployment and going into console, manually adding back in the endpoint, and waiting for it to propogate.

If this is fixed, what are we missing here? I see a PR in release 1.38.0 for the SAM translation that adds in the 'Mode' property - which would help - but what's the fix that you referenced being complete earlier this month?

bkitch21 avatar Jul 16 '21 15:07 bkitch21

++ @Akash Verma @.***>

On Fri, Jul 16, 2021 at 9:05 PM bkitch21 @.***> wrote:

@sapessi https://github.com/sapessi You mention this being fixed and it completing deployment earlier this month. AWS Support is saying that this is only fixed for stacks created after March 29, 2021 and that stacks created before then you have to manually specify 'Merge' as the Mode in the properties on the template. However 'Mode' does not exist in SAM as a property at the moment. Their solution from there was to manually add back in the endpoint ID after deploying via the console. However, this means that on production deployments all of our private API gateways will have downtime between the deployment and going into console, manually adding back in the endpoint, and waiting for it to propogate.

If this is fixed, what are we missing here? I see a PR in release 1.38.0 for the SAM translation that adds in the 'Mode' property - which would help

  • but what's the fix that you referenced being complete earlier this month?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-sam-cli/issues/2353#issuecomment-881537156, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALZFUTKOEKDZAPG2ONICQIDTYBGULANCNFSM4TK7NFQA .

RONITKHUNGAR10 avatar Jul 16 '21 16:07 RONITKHUNGAR10

Fyi, Mode has now been added to SAM (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-mode).

If you explicitly specify Mode: merge it should no longer remove VpcEndpoints.

Make sure to update your sam-cli to get the latest version.

brew upgrade aws-sam-cli

andrewhopper avatar Aug 05 '21 22:08 andrewhopper

Hello @hawflau and @sapessi! We are seeing this issue yet again since last two days (or at least that's when we noticed it in our lower environments), did something change about this?

If I understand correctly, the Mode property mentioned by @andrewhopper is only needed if OpenAPI is used to define the API. The example in the original issue comment doesn't use OpenAPI, so it should not be needed, right?

Or due to translation to CF, it becomes a required property?

saurabhshri avatar Aug 12 '21 08:08 saurabhshri