Skip to main content

A CDK (v2) Construct Library for Secure REST APIs

Project description

aws-cdk-secure-api

https://img.shields.io/pypi/v/aws-cdk-secure-api.svg https://img.shields.io/pypi/pyversions/aws-cdk-secure-api.svg https://github.com/rnag/aws-cdk-secure-api/actions/workflows/dev.yml/badge.svg Documentation Status Updates

An unofficial AWS CDK v2 Construct Library for Secure REST APIs.

Secure Rest Api

The SecureRestApi construct creates a (public) REST API secured behind an API key, which needs to be specified in the x-api-key header for all requests.

Features

  • A CDK Construct which sets up a RestApi secured behind an API key.

  • An API key is auto-generated and stored in SSM Parameter Store (which is a free service) as needed.

  • Local cache for the API key, so that API calls are not needed in future CDK deployments.

  • Helper methods for SecureRestApi, to make it easier to integrate a method for an AWS Lambda function for example.

Usage

The SecureRestApi construct represents a Secure REST API in Amazon API Gateway.

Use add_resource, add_lambda_methods, and add_methods to configure the API model, as shown below.

from aws_cdk_secure_api import SecureRestApi
from aws_cdk import (aws_apigateway as apigw, aws_lambda as lambda_)

get_handler = lambda_.Function(self, 'lambda1', runtime=lambda_.Runtime.PYTHON_3_9, ...)
put_handler = lambda_.Function(self, 'lambda2', runtime=lambda_.Runtime.PYTHON_3_9, ...)

api = SecureRestApi(self, 'api', rest_api_name='My Secure Service')

api.add_lambda_methods(get_handler, 'GET')
api.add_lambda_methods(put_handler, 'PUT', 'POST')

AWS Profile

Note that if you normally pass the --profile to the cdk tool, for example such as:

cdk deploy --profile my-aws-profile

The CDK construct won’t be able to detect the AWS profile in this particular case. A few workarounds can be used for this:

  1. The environment variable AWS_PROFILE can be set before calling the cdk tool.

  2. The profile attribute can be passed in to the config parameter for SecureRestApi.

  3. The profile context variable can be passed in to the cdk tool, as shown below:

    cdk deploy --profile my-profile -c profile=my-profile

API Keys

Here is the process that the CDK construct uses for generating or using an API key for a REST API.

  1. First, it tries to read the API key from local cache, which is located in your home directory, under ~/.cdk/cache/apigw_api_keys.json.

  2. If an API key is found, then it proceeds to use the cached key value, and does not perform the following steps.

  3. An API call is made to read the key from AWS SSM Parameter Store. The param name is /{STACK NAME}/api-key, where {STACK NAME} is the name of the CDK stack.

  4. If the parameter does not exist, an random API key value is auto-generated, and a new SSM Parameter is created in the same AWS account and region that the CDK stack is deployed to.

  5. The API key value is then cached on the local drive, under the ~/.cdk/cache folder.

Stack Outputs

The following stack outputs will additionally be added to the CDK stack:

  • APIEndpoint - The base endpoint of the Secure REST API.

    • Note: this output will not show up if override_endpoint_name is disabled in the config parameter.

  • APIKey - The API key for the endpoint, which needs to be specified as a value in an HTTP request’s x-api-key header.

Credits

This package was created with Cookiecutter and the rnag/cookiecutter-pypackage project template.

History

0.1.0 (2022-06-24)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk-secure-api-0.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

aws_cdk_secure_api-0.1.0-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aws-cdk-secure-api-0.1.0.tar.gz.

File metadata

  • Download URL: aws-cdk-secure-api-0.1.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for aws-cdk-secure-api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d1f7a37b0ff937a6d4d15751daa998ef0a5e0a53c50506c90d62b3cfc7ff1eb6
MD5 1ea1921851c18c060b11b2a2d31431b4
BLAKE2b-256 9400f8d78482b31e0f20801a0e1b2ff66423b1b06fb05ed4d6c8f00126e80f6d

See more details on using hashes here.

File details

Details for the file aws_cdk_secure_api-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for aws_cdk_secure_api-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d9482d94d29efe9efab0c10a7f492752a91498904be6d9ca8dc1cdb5b36f66cd
MD5 44f522b88ed00aa33a12f4e0cfbac201
BLAKE2b-256 b02b0aa859b53c4744703a54781225837f5013a6a66f6ba46dd25ce7a890632d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page