Skip to content

eliran-oraganization/terraform-aws-rest-api-architecture

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The project goal is to make the developer's life easier from the perspective of repetitive DevOps tasks.

When using this project you will be able to create AWS Infrastructure for REST API applications, both server, and client.

This project use terraform to automate the process of creating and maintaining the infrastructure.

Architecture

  1. Application -

    • Server example app includes connection to Atlas MongoDB

    • Client example app includes Sign in with MFA, Read/Write events to server API.

  2. Certificate - Both the server and the client can use a custom domain.

  3. Authentication - Cognito implementation for user management including MFA.

  4. Mongodb - Creation of infrastructure for DB and connect by PrivateLink to AWS infrastructure.

  5. Turrgrant - Easy way to duplicate infrastructure for multiple environments.

  6. CI/CD - Auto deployment process by AWS CodePipeline.

  7. Security - Each resource is protected by WAF rules.

Architecture

Security & Compliance

Security scanning is graciously provided by Bridgecrew. Bridgecrew is the leading fully hosted, cloud-native solution providing continuous Terraform security and compliance.

Benchmark Description
Codacy Badge
Infrastructure Tests Infrastructure Security Compliance
Infrastructure Tests Center for Internet Security, KUBERNETES Compliance
CIS AWS Center for Internet Security, AWS Compliance
CIS AZURE Center for Internet Security, AZURE Compliance
PCI-DSS Payment Card Industry Data Security Standards Compliance
NIST-800-53 National Institute of Standards and Technology Compliance
ISO27001 Information Security Management System, ISO/IEC 27001 Compliance
SOC2 Service Organization Control 2 Compliance
CIS GCP Center for Internet Security, GCP Compliance
HIPAA Health Insurance Portability and Accountability Compliance

Installation

AWS CLI installation

Installation Docs link

brew install awscli

Configure AWS CLI

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format  [None]: json

Terraform installation

Installation Docs (link)

First, install the HashiCorp tap, a repository of all our Homebrew packages.

brew tap hashicorp/tap

Now, install Terraform with hashicorp/tap/terraform.

brew install hashicorp/tap/terraform

Terragrunt installation

Installation Docs link

brew install terragrunt


 
 

Usage

 

Github configuration

Before running the examples you will need to create a Github Personal Access Token that will give terraform access to your repository to be able connecting the repository with AWS Codepipline (CI/CD).

You can follow this link to create a new Access Token.

 

After creating the github secret you need to save it in the AWS Secret Manager service.

The name must be: github_secret

image description

In the secret key use the same key: GitHubPersonalAccessToken

And the value will be your key you generated from Github.

image description


 
 

After this stage terraform will be able to connect the github repository with your project.

Atlas Mongodb configuration

  1. Create Mongodb atlas account. doc link
  2. Create organization API key. doc link
  3. Copy the private and public keys.
  4. Create sensitive.tf file inside the example folder in this project.

The file should look like this:

 

sensitive.tf

public_key="eeeeee"
private_key="aaaaaa-bbbb-cccc-54342-5432ggg"
atlas_org_id = "324214321432aaa"

 

This example will use the minimum required variables to create the infrastructure. This example doesn't include the ACM custom domain.

simple.tf

module "infrastructure" {
  source  = "MoveoTech/api-aws/rest"
  version = "0.0.2"

 region                       = "eu-west-3"
  availability_zones           = ["eu-west-3a"]
  stage                        = "develop"
  name                         = "terraform-moveohls"
  cognito_default_user_email   = "[email protected]"
  client_repository_name       = "terraform-aws-rest-api-architecture"
  client_branch_name           = "main"
  server_repository_name       = "terraform-aws-rest-api-architecture"
  server_branch_name           = "main"
  github_org                   = "MoveoTech"
  private_endpoint_enabled     = true
  enable_atlas_whitelist_ips   = true
  public_key                   = var.public_key
  private_key                  = var.private_key
  atlas_org_id                 = var.atlas_org_id
  provider_instance_size_name  = "M10"
  extended_ec2_policy_document = data.aws_iam_policy_document.service.json
  cognito_enabled              = true
  codebuild_server_env_vars = [{
    name  = "ENV1"
    value = "this is my value"
    type  = "PLAINTEXT"
    },

    {
      name  = "ENV2"
      value = "this is my second value"
      type  = "PLAINTEXT"
    }
  ]
  codebuild_client_env_vars = [{
    name  = "ENV1"
    value = "this is my value"
    type  = "PLAINTEXT"
    },

    {
      name  = "ENV2"
      value = "this is my second value"
      type  = "PLAINTEXT"
    }
  ]
}

 

This example will use a custom domain and all configurable variables.

complete.tf

module "infrastructure" {
  source  = "MoveoTech/api-aws/rest"
  version = "0.0.2"

  region                     = "eu-west-3"
  availability_zones         = ["eu-west-3a"]
  instance_type              = "t3.micro"
  stage                      = "test"
  name                       = "terraform-moveo"
  cognito_default_user_email = "[email protected]"
  client_repository_name     = "terraform-rest-api-aws"
  client_branch_name         = "main"
  server_repository_name     = "terraform-rest-api-aws"
  server_branch_name         = "main"
  github_org                 = "MoveoTech"
  public_key                 = var.public_key
  private_key                = var.private_key
  atlas_org_id               = var.atlas_org_id
  module                     = var.module

  atlas_users                = ["[email protected]"]
  private_endpoint_enabled   = true
  enable_atlas_whitelist_ips = false
  atlas_whitelist_ips        = []

  client_logout_urls          = ["https://www.test.terraform.moveodevelop.com/logout"]
  client_default_redirect_uri = "https://www.test.terraform.moveodevelop.com"
  client_callback_urls        = ["https://www.test.terraform.moveodevelop.com"]


  parent_zone_id            = "ZZG2X8KI3MIQB"
  aliases_client            = ["test.terraform.moveodevelop.com", "www.test.terraform.moveodevelop.com"]
  domain_name               = "test.terraform.moveodevelop.com"
  subject_alternative_names = ["www.test.terraform.moveodevelop.com"]
  dns_alias_enabled         = true
}

Requirements

Name Version
terraform >= 0.13.0
aws >= 3.74.2
mongodbatlas >= 1.3.1

Providers

Name Version
aws >= 3.74.2

Inputs

Name Description Type Required
atlas_org_id The ID of your MongoDB Atlas organization string yes
availability_zones List of availability zones for the selected region list(string) yes
client_branch_name Client branch name string yes
client_repository_name Client repository name string yes
cognito_default_user_email This is a default user to be able to login to the system string yes
github_org Github organization name string yes
private_key The private API key for MongoDB Atlas string yes
public_key The public API key for MongoDB Atlas string yes
region aws region to deploy to string yes
server_branch_name Server branch name string yes
server_repository_name Server repository name string yes
additional_tag_map Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string) no
aliases_client List of FQDN's - Used to set the Alternate Domain Names (CNAMEs) setting on Cloudfront list(string) no
atlas_users List of emails for all the developer who needs access to this organization project list(string) no
attributes ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string) no
autoscale_max Maximum instances to launch number no
autoscale_min Minumum instances to launch number no
client_buildspec_path Full path to the place where the buildspec.yml for client ci string no
client_callback_urls List of allowed callback URLs for the identity providers list(string) no
client_default_redirect_uri The default redirect URI. Must be in the list of callback URLs string no
client_env_prefix Prefix for react envoiremnt variable, this added for supporting NX prefix string no
client_logout_urls List of allowed logout URLs for the identity providers list(string) no
codebuild_client_env_vars Map of custom ENV variables to be provided to the application running on Elastic Beanstalk, e.g. env_vars = { DB_USER = 'admin' DB_PASS = 'xxxxxx' } list(object({ name = string, value = string, type = string })) no
codebuild_server_env_vars Map of custom ENV variables to be provided to the application running on Elastic Beanstalk, e.g. env_vars = { DB_USER = 'admin' DB_PASS = 'xxxxxx' } list(object({ name = string, value = string, type = string })) no
cognito_enabled Allow cognito authorization on api gateway routes bool no
context Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any no
delimiter Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
string no
descriptor_formats Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br> format = string<br> labels = list(string)<br>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any no
dns_alias_enabled Create a DNS alias for the CDN. Requires parent_zone_id or parent_zone_name bool no
domain_name A domain name for which the certificate should be issued string no
enable_atlas_whitelist_ips Enable the whitelist ip, if it enabled the ip's taken from the AWS EIP and allow this ip access to atlas cluster bool no
enabled Set to false to prevent the module from creating any resources bool no
env_vars Map of custom ENV variables to be provided to the application running on Elastic Beanstalk, e.g. env_vars = { DB_USER = 'admin' DB_PASS = 'xxxxxx' } map(string) no
extended_ec2_policy_document Extensions or overrides for the IAM role assigned to EC2 instances string no
id_length_limit Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
number no
instance_type Instances type string no
label_key_case Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
string no
label_order The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string) no
label_value_case Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
string no
labels_as_tags Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string) no
name ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
string no
namespace ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique string no
parent_zone_id The id of the parent Route53 zone to use for the distribution. string no
private_endpoint_enabled Private endpoint allow to connect between 2 aws accounts by private network no need to use internet. To use this feature you need to add payment card to your atlas account bool no
provider_instance_size_name Atlas provides different instance sizes, each with a default storage capacity and RAM size string no
regex_replace_chars Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
string no
server_buildspec_path Full path to the place where the buildspec.yml for server ci string no
stage ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' string no
subject_alternative_names A list of domains that should be SANs in the issued certificate list(string) no
tags Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string) no
tenant ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for string no

Outputs

Name Description
atlas_project_id ID's of the created cluster project
client_url Client url
server_url Server api url

About

REST API architecture baseline

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HCL 93.3%
  • TypeScript 3.8%
  • Python 1.2%
  • Other 1.7%