Giter VIP home page Giter VIP logo

rogeriomm / aws-lab Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 8.0 10.65 MB

Terraform AWS free tier, EC2/ECR/RDS/EFS/DynamoDB/Lambda/S3. Docker running on EC2, Traefik reverse proxy, Lets Encrypt, dynamic DNS, OpenVPN(TODO)

HCL 76.57% Python 1.14% Go 4.69% Makefile 0.63% Java 15.09% Rust 1.87%
aws aws-free-tier infrastructure-as-code terraform traefik aws-ec2 aws-lambda docker dynamic-dns lets-encrypt

aws-lab's Introduction

Terraform AWS Free Tier

Getting started with the Terraform for managing a base free-tier AWS resources.

Project description

This is a Terraform project for managing AWS resources.

It can build the next infrastructure:

Architecture Diagram

Install

Pre steps

  1. Install software
    • Install infrastructure tools
brew install terraform awscli
  • Install general development tools
brew install make yq zip curl gradlle
  • Install PYTHON
brew install pyenv
  • Install GO
brew install golang
  • Install JAVA
brew install java11 jenv
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add x86_64-unknown-linux-gnu
brew tap cargo-lambda/cargo-lambda
brew install cargo-lambda
  1. Create AWS account
  2. If the file ~/.aws/credentials doesn't exist, create it and add you Terraform profile to the file. For example:
   [terraform]
   aws_access_key_id = Your access key
   aws_secret_access_key = Your secret access key 
  1. Check AWS account
aws sts get-caller-identity
  1. Create S3 bucket to store Terraform state
aws s3api create-bucket --bucket world-terraform --region us-east-1
  1. Create config file config.tf that will contain information how to store state in a given bucket. See example.

  2. Create SSH key pair to connect to EC2 instance:

cd ./src/free-tier/provision/access

# it creates "free-tier-ec2-key" private key and "free-tier-ec2-key.pub" public key
ssh-keygen -f free-tier-ec2-key
  1. Adds SSH private key identities to the authentication agent
ssh-add src/free-tier/provision/access/free-tier-ec2-key

Build infrastructure

  • Install Python
pyenv install 3.8.16
cd ./src/free-tier/python
pyenv local 3.8.16
  • Install Java
jenv enable-plugin gradle
  • Install RUST lambda
brew tap cargo-lambda/cargo-lambda
brew install cargo-lambda
  • Build lambda samples
cd ./src/free-tier/lambda/samples
make all
cd ./src/free-tier
terraform init -backend-config="./backend/config.tf"
cd ./src/free-tier
terraform plan
cd ./src/free-tier
terraform apply

Post install

ip=$(aws ec2 describe-instances | 
      yq 'select(.Reservations[].Instances[].State.Code == 16) | .Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddresses[].Association.PublicIp')
echo $ip
  • Edit /etc/hosts add "aws" host name
sudo bash -c "echo $ip aws >> /etc/hosts"
  • Add SSH public key from EC2 server
ssh-keygen -R aws
ssh-keyscan -H aws >> ~/.ssh/known_hosts
  • Install EPEL, Postgres 14 packages
ssh ec2-user@aws "sudo amazon-linux-extras install epel postgresql14 -y"
  • Upgrade Linux and install packages
ssh ec2-user@aws "sudo yum update && sudo yum upgrade -y && sudo yum install -y netcat openvpn postgresql docker python3-pip htop"
  • Install Docker compose, configuration
ssh ec2-user@aws "sudo usermod -a -G docker ec2-user && sudo pip3 install docker-compose"
ssh ec2-user@aws "sudo systemctl enable docker.service && sudo systemctl start docker.service && systemctl status docker.service"
  • Edit src/docker/env-duckdns.sh
SUBDOMAINS=your-subdomain
DUCKDNS_TOKEN=your-token
TOKEN=$DUCKDNS_TOKEN
  • Edit src/docker/docker-compose.yaml, set email and Duckdns subdomain TODO

  • Edit src/docker/conf/users.txt TODO

  • Copy Docker files

ssh ec2-user@aws "mkdir -p docker/conf"
scp src/docker/docker-compose.yaml src/docker/env-duckdns.sh ec2-user@aws:./docker/
scp src/docker/conf/dynamic_conf.yml ec2-user@aws:./docker/conf/dynamic_conf.yml
scp src/docker/conf/users.txt ec2-user@aws:./docker/conf/users.txt
  • Start Docker containers
ssh ec2-user@aws "cd docker && docker-compose up -d"

Services

Duckdns

SUBDOMAINS=sub-domain-1,sub-domain-2,sub-domain-3,sub-domain-4,sub-domain-5
DUCKDNS_TOKEN=your-token
TOKEN=your-token
  • Duckdns logs
ssh ec2-user@aws "cd docker && docker-compose logs duckdns"

Traefik

  • Traefik logs
ssh ec2-user@aws "cd docker && docker-compose logs traefik"

AWS RDS

aws rds describe-db-instances | yq
  • Get Postgres endpoint
address=$(aws rds describe-db-instances | yq '.DBInstances[] | select(.DBName=="labdb") | .Endpoint.Address')
port=$(aws rds describe-db-instances | yq '.DBInstances[] | select(.DBName=="labdb") | .Endpoint.Port')
echo $address:$port
  • Check Postgres routing from EC2
ssh ec2-user@aws "nc -v $address $port"
  • Postgres cli
ssh ec2-user@aws psql --host $address --port $port --username postgres

AWS DynamoDB

aws dynamodb list-tables | yq

AWS EFS

aws efs describe-file-systems | yq
aws efs describe-access-points | yq
filesystem_id=$(aws efs describe-file-systems | yq '.FileSystems[] | select(.Name=="free-tier-efs") | .FileSystemId')
echo $filesystem_id
aws efs describe-mount-targets --file-system-id $filesystem_id | yq
  • Get NFS IP address
nfs_ip=$(aws efs describe-mount-targets --file-system-id $filesystem_id | yq '.MountTargets[] | select (.AvailabilityZoneName=="us-east-1a") | .IpAddress')
echo $nfs_ip
  • Check NFS routing from EC2
ssh ec2-user@aws "nc -v $nfs_ip 2049"

AWS API Gateway

aws apigatewayv2 get-apis | yq
endpoint=$(aws apigatewayv2 get-apis | yq '.Items[] | select (.Name=="free-tier-api-gateway") | .ApiEndpoint')
echo $endpoint
  • Run lambda Python
curl "$endpoint/api/python"

Open VPN

Destroy infrastructure

cd ./src/free-tier
terraform destroy

Links to similar projects

Links

aws-lab's People

Contributors

rogeriomm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.