2

I want to deploy Lambda + API-gateway + CloudFront through serverless framework.

Lambda and API-gateway can be deployed by defining function in serverless.yml and for CloudFront I define resources.

However, for CloudFront, I had to define DomainName attribute which should be filled with API-gateway endpoint that is not yet deployed.

Resources:
  ApiDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
        - Id: ApiGateway
          DomainName: "api-gateway endpoint"

As the result, I have to follow the steps.

  1. Deploy Lambda + API-gateway.
  2. Check API-gateway endpoint.
  3. Add CloudFront resource into serverless.yml with DominName attribute pointing to API-gateway endpoint.
  4. Deploy CloudFront.

Is there any way to deploy all(CloudFront + API-gateway + Lambda) at a time? (But I don't want to give API-gateway DNS)

2 Answers 2

1

You could use serverless-api-cloudfront which automatically creates properly configured AWS CloudFront distribution that routes traffic to API Gateway. To use it, it's simple, you have to install it using npm i --save-dev serverless-api-cloudfront, after that you have to add in your serverless.yml file:

plugins:
  - serverless-api-cloudfront

custom:
  apiCloudFront:
    domain: my-custom-domain.com
    certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
    waf: 00000000-0000-0000-0000-000000000000
    compress: true
    logging:
      bucket: my-bucket.s3.amazonaws.com
      prefix: my-prefix
    cookies: none
    headers:
      - x-api-key
    querystring:
      - page
      - per_page

If you which to understand how it works, and other possible configurations, you may visit their Github's page.

Sign up to request clarification or add additional context in comments.

2 Comments

It still looks to have to put api-gateway endpoint into domain attribute in your example. right? How could I know API-gateway endpoint that is not yet deployed?
@SangminKim The domain value should be your custom domain, not the endpoint provided my API Gateway.
1

You can refer to any resources created by serverless using the same way you would refer them in cloudformation.

Serverless framework creates the cloudformation template to deploy - Sls package - cd .serverless - cat cloudformation-template-update-stack.json

You can get the name of the resource that sls creates for you

DomainName:
            Fn::Join:
              - "."
              - - Ref: ApiGatewayRestApi
              - execute-api
              - - Ref: AWS::Region
              - amazonaws.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.