All Projects → dpetzold → terraform-aws-zappa

dpetzold / terraform-aws-zappa

Licence: Apache-2.0 License
Create a AWS VPC with associated resources for use with Zappa

Programming Languages

HCL
1544 projects

Projects that are alternatives of or similar to terraform-aws-zappa

Autospotting
Saves up to 90% of AWS EC2 costs by automating the use of spot instances on existing AutoScaling groups. Installs in minutes using CloudFormation or Terraform. Convenient to deploy at scale using StackSets. Uses tagging to avoid launch configuration changes. Automated spot termination handling. Reliable fallback to on-demand instances.
Stars: ✭ 2,014 (+6613.33%)
Mutual labels:  aws-lambda, terraform-module
Zappa
Serverless Python
Stars: ✭ 11,859 (+39430%)
Mutual labels:  aws-lambda, zappa
chocs
Modern HTTP framework for AWS Serverless and WSGI compatible servers.
Stars: ✭ 15 (-50%)
Mutual labels:  aws-lambda
rust-wasm-on-lambda-edge
Rust/WASM on AWS Lambda@Edge (CloudFront)
Stars: ✭ 12 (-60%)
Mutual labels:  aws-lambda
lambda-checklist
The checklist for meticulous serverless engineers
Stars: ✭ 26 (-13.33%)
Mutual labels:  aws-lambda
SWoT
A web application for designing, tracking, and reporting progress on workouts. Written in JavaScript with React, Redux, Material UI, and hosted by a serverless AWS backend.
Stars: ✭ 14 (-53.33%)
Mutual labels:  aws-lambda
jschr.io
The static website generator service behind jschr.io.
Stars: ✭ 70 (+133.33%)
Mutual labels:  aws-lambda
super-serverless-sample
Backend serverless que simula o sistema de votação do BBB
Stars: ✭ 30 (+0%)
Mutual labels:  aws-lambda
eksphemeral
A simple Amazon EKS manager for ephemeral clusters
Stars: ✭ 68 (+126.67%)
Mutual labels:  aws-lambda
hyper-kube-config
H Y P E R K U B E - A Serverless API and kubectl plugin providing a storage and retrieval Kubernetes cluster credentials. Hyperkube leverages AWS Secrets Manager for storing credential information.
Stars: ✭ 27 (-10%)
Mutual labels:  aws-lambda
nuxt-on-lambda
Nuxt.jsをAWS Lambdaで動かす
Stars: ✭ 78 (+160%)
Mutual labels:  aws-lambda
nestjs-graphql-serverless
Boilerplate for using NestJS with GraphQL (Code-First) on serverless environment (AWS Lambda)
Stars: ✭ 64 (+113.33%)
Mutual labels:  aws-lambda
bref-symfony-demo
[DEPRECATED] Demo of a Symfony application deployed on AWS lambda using Bref
Stars: ✭ 32 (+6.67%)
Mutual labels:  aws-lambda
mangum-cli
CLI tools for use with Mangum
Stars: ✭ 14 (-53.33%)
Mutual labels:  aws-lambda
whats-your-name
Sample app for AWS Serverless Repository - uses Amazon Rekognition to recognize person on the photo
Stars: ✭ 17 (-43.33%)
Mutual labels:  aws-lambda
terraform-aws-account
🌳 A sustainable Terraform Package which creates Account & IAM resources on AWS
Stars: ✭ 18 (-40%)
Mutual labels:  terraform-module
amazon-ivs-ecommerce-web-demo
This repository shows how you can build a compelling eCommerce experience with Amazon IVS.
Stars: ✭ 19 (-36.67%)
Mutual labels:  aws-lambda
combinator
Combinator.ml's central repo, documentation and website
Stars: ✭ 24 (-20%)
Mutual labels:  terraform-module
Hodor
Hodor is a an automation application that is used to open the door controlled by an intercom system from Slack using a custom slash command.
Stars: ✭ 21 (-30%)
Mutual labels:  aws-lambda
serverless-transformers-on-aws-lambda
Deploy transformers serverless on AWS Lambda
Stars: ✭ 100 (+233.33%)
Mutual labels:  aws-lambda

Zappa AWS Module

Introduction

This module creates a VPC with the following resources to quickly get you up and running with Zappa:

Architecture

It is based on following guides:

Please see them for the motivations behind this module.

Sample output

After the vpc module is run the output similar to the one below will be generated:

bastion_public_ip = 34.34.34.34
default_security_group_id = sg-bcbcbcbc
lambda_subnet_ids = [
    subnet-d1111111,
    subnet-cc222222,
    subnet-00000000
]
nat_private_ips = [
    10.10.11.55
]
postgres_cname = postgres.internal
postgres_password = <password>
redis_cname = redis.internal

Getting Started

This module is split into two parts as Zappa needs to be deployed after the VPC is created but before the custom domain and CloudFront distribution are created. Which gives us three phases:

  1. Create the VPC and internal resources
  2. Deploy the Zappa application
  3. Create the custom domain and CloudFront distribution

The instructions below will walk you through deploying a Django application with terraform and Zappa.

Setup

This guide requires that you have terraform and the aws cli configured and working. See:

You will at least need to set the following:

$ export AWS_ACCESS_KEY_ID="anaccesskey"
$ export AWS_SECRET_ACCESS_KEY="asecretkey"
$ export AWS_DEFAULT_REGION="us-east-1"

To verify:

$ aws ec2 describe-vpcs

Copy the files in the examples to a location for editing:

mkdir -p <yourproject>/terraform/vpc
cp -R examples/vpc/simple/* <yourproject>/terraform/vpc
cp -R examples/cloudfront <yourproject>/terraform

This module assumes you are using a Route53 hosted zone for DNS. If you are not using Route53 you must create a zone and copy the resulting records to your authoritative source.

Phase 1 - Create the VPC

Create the terraform configuration

Edit:

vpc/main.tf
module "vpc" {
  source           = "github.com/dpetzold/terraform-aws-zappa/vpc"
  name             = "lambda"
  aws_key_name     = "lambda"
  aws_key_location = "${file(./private-key)}"
}

The name variable is used to name the vpc and the resources in it. The aws_key_* variables are required to for ssh access to the bastion and NAT instances.

Create the keypair

https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#KeyPairs:sort=keyName

Download the PEM file to a location accessible to the module.

Run terrafrom

$ cd vpc
$ terraform init
$ terraform plan
$ terraform apply

Setup SSH

Edit your ssh configuration file to provide access to the EC2 instances:

.ssh/config
Host bastion
  Hostname <bastion-public-ip>
  User ubuntu
  IdentityFile <pathtopemfile>

Host nat1
  Hostname <nat-private-ip>
  User ubuntu
  IdentityFile <pathtopemfile>
  ProxyJump bastion

Initialize the database

$ scp <sqlfile> bastion:
$ ssh bastion
$ pg_restore -h postgres.internal -U postgres -W -C -d postgres <sqlfile>

Phase 2 - Deploy with Zappa

Update Django settings

CloudFront is configured to pass the Host field via the X-Forwarded-Host header. It is required by Django for host header validation and the sites framework. Update the settings file so the forwarded field is used.

USE_X_FORWARDED_HOST = True

Deploy Zappa

Update the Zappa settings file with the subnet ids and default security group from the terraform output.

zappa_settings.json
{
    "prod": {
        "django_settings": "config.settings.production",
        "aws_region": "us-east-1",
        "runtime": "python3.6",
        "s3_bucket": "lambda-example-com",
        "domain": "api.example.com",
        "aws_environment_variables": {
            "BROKER_URL": "redis://redis.internal:6379/4",
            "CELERY_RESULT_URL": "redis://redis.internal:6379/5",
            "DATABASE_URL": "postgres://postgres:<dbpassword>@postgres.internal/postgres",
            "DJANGO_AWS_REGION": "us-east-1",
            "DJANGO_SETTINGS_MODULE": "config.settings.production"
        },
        "vpc_config": {
            "SubnetIds": [
                "subnet-d1111111",
                "subnet-cc222222",
                "subnet-00000000"
            ],
            "SecurityGroupIds": [
                "sg-bcbcbcbc"
            ]
        }
    }
}

Then run:

zappa deploy prod

Phase 3 - Create the custom domain and CloudFront distribution

Create the SSL certificate

https://console.aws.amazon.com/acm/home?region=us-east-1#/

Get the api id

$ aws apigateway get-rest-apis --query 'items[0].id'
"8vpos55555"

Create the terraform configuration

Edit:

cloudfront/main.tf
module "lambda" {
  source       = "github.com/dpetzold/terraform-aws-zappa/cloudfront"
  domain_names = ["example.com"]
  stage_name   = "prod"
  api_id       = "yourapiid"
}

The domain_names variable is used to create the CloudFront distribution and it is the key used to pull the SSL certificate. A CloudFront distribution is created for each specified domain. This would be used if you have many domains pointed to a single Django application. The stage_name and api_id variables are used when creating the custom domain. The custom domain is named api and is tertiary to the first domain name. For example, api.example.com. It is what the CloudFront distribution will point to.

Create the Custom Domain and CloudFront Distribution

$ cd cloudfront
$ terraform init
$ terraform plan
$ terraform apply

It can take up to 15 minutes for the CloudFront distribution to provision.

Taking it down

To destroy the resources created run the following:

$ cd cloudfront
$ terraform destroy

$ cd ../..
$ zappa undeploy prod

$ cd terraform/vpc
$ terraform destroy

License

This code is released under the Apache 2.0 License. Please see LICENSE and for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].