2

I have written code as

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    sam-app

    Sample SAM Template for sam-app

Globals:
    Function:
        Timeout: 300
    Api:
        Cors:
            AllowHeaders: "'content-type, authorization'"
            AllowOrigin: "'*'"


Resources:

    HelloWorldFunction:
        Type: AWS::Serverless::Function 
        Properties:
            CodeUri: hello_world
            Handler: app.lambda_handler
            Runtime: nodejs8.10
            Environment: 
                Variables:
                    PARAM1: VALUE
            Events:
                HelloWorld:
                    Type: Api 
                    Properties:
                        Path: /hello
                        Method: get


    ApiGateway:
        Type: AWS::Serverless::Api
        Properties:
            StageName: prod
            DefinitionBody:
                swagger: "2.0"
                info:
                    title: 
                    Ref: AWS::StackName
                    description: My API that uses custom authorizer
                    version: 1.0.0
                paths:
                    /hello:
                        get:
                            consumes: application/json
                            produces: application/json

I want to deploy this code to cloud as

aws cloudformation deploy --template-file output.yaml --stack-name stack1 --parameter-overrides EnvParameter=prod --capabilities CAPABILITY_IAM

Then it says

Failed to create/update the stack. Run the following command
to fetch the list of events leading up to the failure
aws cloudformation describe-stack-events --stack-name stack1

When I comment the ApiGateway code, it is working fine. I think the error may be in below part of the code.

ApiGateway:
        Type: AWS::Serverless::Api
        Properties:
            StageName: prod
            DefinitionBody:
                swagger: "2.0"
                info:
                    title: something
                    Ref: AWS::StackName
                    description: My API that uses custom authorizer
                    version: 1.0.0
                paths:
                    /hello:
                        get:
                            consumes: application/json
                            produces: application/json

Please help me solve this issue.

Thank you...

Sai M.
  • 2,548
  • 4
  • 29
  • 46
  • The line with "Ref: AWS::StackName" looks odd. That's probably no valid swagger template. If you want to use the stack name as part of the title, you should probably do it like in the following example: https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/inline_swagger/template.yaml#L12-L13 – Dunedan Jul 06 '18 at 16:35
  • @Dunedan even after giving a hard-coded title as `title: something`, it is showing error as same above. – Sai M. Jul 09 '18 at 05:01
  • In your initial code the indentation for the method definition is wrong (`/hello:` and `get:` at the same level. That's probably a problem, but I'm slightly confused as it's properly intended in the other quoted snippet. – Dunedan Jul 09 '18 at 05:11
  • That was not the issue. The mistake happened while posting the question. I updated the question. Please help me what is the other mistake. – Sai M. Jul 09 '18 at 05:50
  • 1
    Have you checked the CloudFormation console regarding a more speciifc error message? – Dunedan Jul 09 '18 at 05:57
  • @Dunedan it says `Invalid Swagger 2.0 input. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 6fb7e100-833f-11e8-ac64-351a68a3488f)`. Where is the exact error not understood. – Sai M. Jul 09 '18 at 06:22
  • The missing `x-amazon-apigateway-integration` (https://github.com/awslabs/serverless-application-model/blob/e8f74f56d19af0b5d0e3e534137a7a875f7d1f7a/examples/2016-10-31/inline_swagger/template.yaml#L17-L21) could be the culprit. Also note, that you haven't referenced the API in the event configuration of your Lambda function. – Dunedan Jul 09 '18 at 06:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174627/discussion-between-saiunique-and-dunedan). – Sai M. Jul 09 '18 at 07:02

2 Answers2

3

This may be late, but the issue that was causing my cloudformation to fail is that my programmatic user account creating the cloud formation did not have iam:CreateRole permissions. Updating the policies for that user fixed the issue.

trystuff
  • 686
  • 1
  • 8
  • 18
  • Thanks! I cannot believe that they don't indicate that in any amazon tutorial. Can you please provide all the other permissions? I got it working but I used this 4 AWSLambdaFullAccess, AWSCloudFormationFullAccess, IAMFullAccess, and AmazonS3FullAccess, which is obviously bad... – Sfp Sep 01 '20 at 17:42
0

This error can occur due to more than one reason. Few cases amongst them can be

  1. Not having the correct permissions.
  2. Trying to create a resource that is already present in your AWS account.

How to debug such issues.

  1. Go to CloudFormation > Stacks > <your_stack_name>
  2. Inside that go to Events. Here, you can view your resource name and Status reason.
  3. Inside the status reason you can view the error.
  4. Based on the error, try to resolve them accordingly.

You can use the following command to verify the errors. This will log all the details of your resource status.

aws cloudformation describe-stack-events --stack-name <your_stack_name>

For above mentioned reason follow these steps.

  1. Not having the correct permissions.

    • Update your IAM Role policies. Many times we do not have the correct permission to create resources using Cloud Formation and end up facing this error.
  2. Trying to create a resource that is already present in your AWS account.

    • This happens when we try to create a resource with the same name again. Either delete the previous resource manually using the AWS console or try giving a unique name to avoid such errors.

These were a few reasons and debugging strategies to overcome this issue.

sakigo
  • 95
  • 8