PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
No Result
View All Result
Home DevOps

Configuring custom domain names with AWS Chalice : idk.dev

July 22, 2020
in DevOps
289 3
Configuring custom domain names with AWS Chalice : idk.dev


Version 1.16.0 of AWS Chalice, a framework for writing serverless applications in Python, adds support for custom domain names when creating REST and WebSocket APIs. In this post, we’ll show how to associate your own domain name with a REST API we create.

When deploying REST APIs with Chalice, Amazon API Gateway automatically generates the domain name of your API endpoint. Let’s start by creating a Chalice application with a regional endpoint for our rest API so we can see the auto-generated domain name. After that, we’ll associate our custom domain with this API.

First, install Chalice in a virtual environment. We’re using Python 3.7 for this example.

$ python3 -m venv /tmp/venv37
$ . /tmp/venv37/bin/activate
$ pip install chalice
$ chalice new-project customdomain
$ cd customdomain

Next, we’ll change our endpoint to be a REGIONAL endpoint type. The default endpoint type for Chalice applications is EDGE. To do this, update your .chalice/config.json file with the api_gateway_endpoint_type parameter:

{
  "version": "2.0",
  "app_name": "customdomain",
  "api_gateway_endpoint_type": "REGIONAL",
  "stages": {
    "dev": {
      "api_gateway_stage": "api"
    }
  }
}

Now if we deploy our application, we’ll see the auto-generated domain name that API Gateway creates for us.

$ chalice deploy
Creating deployment package.
Creating IAM role: customdomain-dev
Creating lambda function: customdomain-dev
Creating Rest API
Resources deployed:
  - Lambda ARN: arn:aws:lambda:us-west-2:12345:function:customdomain-dev
  - Rest API URL: https://abcd.execute-api.us-west-2.amazonaws.com/api/

This https://abcd.execute-api.us-west-2.amazonaws.com/api/ endpoint is what you’ll configure your HTTP client to use when invoking your REST API.

Now we’ll look at how you can configure a Chalice application to use your own custom domain name.

In order to set this up, we first need to configure our domain name. In this example I’ll be using a domain name of api.chalice-demo-app.com. We’ll be managing our domain name with Amazon Route 53 and we’ll use AWS Certificate Manager (ACM) to manage our SSL/TLS certificate.

You can follow the documentation for requesting a public certificate. You’ll need to ensure that you create your ACM certificate in the same region as your REST API. In this example, I’m using us-west-2. Once you’ve created your ACM certificate, you should have a CertificateArn ARN you can use. Be sure to save this value for later, we’ll need it when configuring our Chalice app.

You’ll also need the hosted zone ID of your domain name in Route53. You can find the hosted zone Id for your domain by running this CLI command and finding the hosted zone that corresponds to your domain name.

$ aws route53 list-hosted-zones
{
    "HostedZones": [
        {
            "Id": "/hostedzone/Z0560960ABCDEFGABCDE",
            "Name": "chalice-demo-app.com.",
            "CallerReference": "1658FD20-6694-E292-8259-DB8B62704A38",
            "Config": {
                "Comment": "",
                "PrivateZone": false
            },
            "ResourceRecordSetCount": 3
        }
    ]
}

With this output above, my hosted zone id is Z0560960ABCDEFGABCDE.

Connecting your custom domain to your Chalice application consists of two parts. First we must configure our API Gateway endpoint so it knows about our custom domain name. This is done through Chalice configuration options. Second, we must configure our custom domain to point to our new application. This is done through Route53.

In order to configure Chalice with this custom domain name, you need to the specify your custom domain name as well as which ACM certificate to use. These values are specified in your .chalice/config.json file. Update your config file as shown below but replace the values of domain_name and certificate_arn with your own values. The certificate_arn should match the ARN of your ACM certificate you created earlier.

{
  "version": "2.0",
  "app_name": "customdomain",
  "api_gateway_endpoint_type": "REGIONAL",
  "stages": {
    "dev": {
      "api_gateway_custom_domain": {
        "domain_name": "api.chalice-demo-app.com",
        "certificate_arn": "arn:aws:acm:us-west-2:12345:certificate/your-uuid"
      },
      "api_gateway_stage": "api"
    }
  }
}

When we now redeploy our application, we’ll see that we’ve now created and associated a domain name with our REST API: Once I’ve done that I can deploy my Chalice application and my REST API will be configured with a new custom domain name:

$ chalice deploy
Creating deployment package.
Updating policy for IAM role: customdomain-dev
Updating lambda function: customdomain-dev
Updating rest API
Updating custom domain name: api.chalice-demo-app.com
Resources deployed:
  - Lambda ARN: arn:aws:lambda:us-west-2:123456789012:function:customdomain-dev
  - Rest API URL: https://abcdefg.execute-api.us-west-2.amazonaws.com/api/
  - Custom domain name:
      HostedZoneId: Z1UJRXOUMOOFQ8
      AliasDomainName: d-ebljg1s242.execute-api.us-west-2.amazonaws.com

Note that a HostedZoneId and an AliasDomainName are now shown in the output. We’ll now use these values to configure our hosted zone in Route53 with an ALIAS record. To do this, we can run this AWS CLI command:

$ aws route53 change-resource-record-sets --hosted-zone-id Z0560960ABCDEFGABCDE --change-batch 
'{
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "api.chalice-demo-app.com",
        "Type": "A",
        "AliasTarget": {
          "DNSName": "d-ebljg1s242.execute-api.us-west-2.amazonaws.com",
          "HostedZoneId": "Z1UJRXOUMOOFQ8",
          "EvaluateTargetHealth": false
        }
      }
    }
  ]
}'

Make sure to use your own values for the Name, DNSName, and HostedZoneId. The HostedZoneId should be the value shown in the output of the chalice deploy command.

If you’re not using Route53 to manage your hosted zone, you’ll need to create a CNAME record that points to the AliasDomainName for your API using your third-party domain registrar.

Our custom domain is now setup. We can now verify that we can make requests to our custom domain.

$ curl https://api.chalice-demo-app.com/
{"hello":"world"}

Next Steps

You can configure custom domain names for a REST API as well as WebSocket APIs. The documentation also shows additional parameters that can be specified when configuring a custom domain. Try out the latest version of Chalice today, and let us know what you think. You can share feedback with us on our GitHub repo.



Source link

Share219Tweet137Share55Pin49

Related Posts

Following serverless best practices with AWS Chalice and Lambda Powertools : idk.dev
DevOps

Following serverless best practices with AWS Chalice and Lambda Powertools : idk.dev

AWS Chalice lets you quickly create serverless applications in Python. It has a number of built-in features such as...

September 23, 2020
Connection UX improvements in the AWS Toolkit for JetBrains : idk.dev
DevOps

Connection UX improvements in the AWS Toolkit for JetBrains : idk.dev

The AWS Toolkit for JetBrains is a plugin for JetBrains’s IDEs that makes it easier to write applications for...

September 23, 2020
Creating an EC2 instance in the AWS Wavelength Zone : idk.dev
DevOps

Creating an EC2 instance in the AWS Wavelength Zone : idk.dev

Creating an EC2 instance in the AWS Wavelength ZoneThis blog post is contributed by Saravanan Shanmugam, Lead Solution Architect,...

September 22, 2020
EFA-enabled C5n instances to scale Simcenter STAR-CCM+ : idk.dev
DevOps

EFA-enabled C5n instances to scale Simcenter STAR-CCM+ : idk.dev

This post was contributed by Dnyanesh Digraskar, Senior Partner SA, High Performance Computing; Linda Hedges, Principal SA, High Performance...

September 21, 2020
Next Post
Liberty IT Adopts Serverless Best Practices Using AWS Cloud Development Kit : idk.dev

Liberty IT Adopts Serverless Best Practices Using AWS Cloud Development Kit : idk.dev

Linux Foundation Announces Advanced Cloud Engineer Bootcamp

Linux Foundation Announces Advanced Cloud Engineer Bootcamp

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Introducing GitHub’s OpenAPI Description – The GitHub Blog

Introducing GitHub’s OpenAPI Description – The GitHub Blog

July 27, 2020
SameSite cookies explained

SameSite cookies explained

May 24, 2020
Proactively Monitoring System Performance on Amazon Lightsail Instances : idk.dev

Proactively Monitoring System Performance on Amazon Lightsail Instances : idk.dev

June 4, 2020
It’s Always Year Zero | CSS-Tricks

It’s Always Year Zero | CSS-Tricks

December 16, 2020

Categories

  • AWS
  • Big Data
  • Database
  • DevOps
  • IoT
  • Linux
  • Web Dev
No Result
View All Result
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In