-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) test role on ephemeral ec2 provisioned w terraform (#1)
- Loading branch information
1 parent
23e7775
commit 778ee21
Showing
13 changed files
with
217 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.terraform | ||
*.tfstate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env make | ||
|
||
export TF_VAR_instance_name=dev_server_role_dev_env | ||
|
||
init: | ||
cd terraform; terraform init; | ||
|
||
apply: | ||
cd terraform; terraform apply; | ||
|
||
destroy: | ||
cd terraform; terraform destroy; | ||
|
||
connect: | ||
./scripts/connect-to-instance.sh | ||
|
||
ansible: | ||
./scripts/ansible.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ docker_packages: | |
- python3-setuptools | ||
|
||
postgres_packages: | ||
- acl | ||
- bash | ||
- openssl | ||
- libpq-dev | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#! /bin/bash | ||
|
||
## | ||
## Use this script to run ansible on your instance after applying terraform | ||
## | ||
## Run via `make ansible` | ||
## | ||
## This script requires $TF_VAR_instance_name to be exported in Makefile | ||
## | ||
|
||
if [[ -z "$TF_VAR_instance_name" ]]; then | ||
echo "Must provide TF_VAR_instance_name in environment" 1>&2 | ||
echo "Run this script via `make connect`" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
export PUBLIC_IP=$(aws ec2 describe-instances --filters Name=instance-state-name,Values=running Name=tag:Name,Values=$TF_VAR_instance_name --region=us-east-2 | jq -r .Reservations[].Instances[].PublicIpAddress) | ||
|
||
echo "$PUBLIC_IP" > "tests/inventory" | ||
|
||
ansible-playbook -i ./tests/inventory --private-key ~/.ssh/lemur-pro.pem ./tests/test.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#! /bin/bash | ||
|
||
## | ||
## Use this script to connect to your instance after applying terraform | ||
## | ||
## Run via `make connect` | ||
## | ||
## This script requires $TF_VAR_instance_name to be exported in Makefile | ||
## | ||
|
||
if [[ -z "$TF_VAR_instance_name" ]]; then | ||
echo "Must provide TF_VAR_instance_name in environment" 1>&2 | ||
echo "Run this script via `make connect`" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
export PUBLIC_DNS=$(aws ec2 describe-instances --filters Name=instance-state-name,Values=running Name=tag:Name,Values=$TF_VAR_instance_name --region=us-east-2 | jq -r .Reservations[].Instances[].PublicDnsName) | ||
|
||
ssh -A ubuntu@$PUBLIC_DNS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
--- | ||
|
||
- import_tasks: tasks/packages.yml | ||
- import_tasks: tasks/tools/git.yml | ||
- import_tasks: tasks/tools/docker.yml | ||
- import_tasks: tasks/tools/postgres.yml | ||
- import_tasks: tasks/tools/oh-my-zsh.yml | ||
- import_tasks: tasks/tools/nodejs.yml | ||
- import_tasks: tasks/filesystem.yml | ||
- import_tasks: tasks/smoke-test.yml | ||
- import_tasks: ../tasks/packages.yml | ||
- import_tasks: ../tasks/tools/git.yml | ||
- import_tasks: ../tasks/tools/docker.yml | ||
- import_tasks: ../tasks/tools/postgres.yml | ||
- import_tasks: ../tasks/tools/oh-my-zsh.yml | ||
- import_tasks: ../tasks/tools/nodejs.yml | ||
- import_tasks: ../tasks/filesystem.yml | ||
- import_tasks: ../tasks/smoke-test.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
terraform { | ||
backend "s3" { | ||
bucket = "benmangold-tf-state-bucket" | ||
key = "globals/s3/terraform.tfstate" | ||
region = "us-east-2" | ||
dynamodb_table = "benmangold-tf-state-lock-table" | ||
encrypt= "true" | ||
} | ||
} | ||
|
||
variable "key_name" { | ||
description = "AWS key name used for SSH access" | ||
type = string | ||
} | ||
|
||
variable "instance_name" { | ||
description = "Value for EC2 Instance 'Name' tag key" | ||
type = string | ||
} | ||
|
||
provider "aws" { | ||
region = "us-east-2" | ||
} | ||
|
||
data "aws_vpc" "default" { | ||
default = true | ||
} | ||
|
||
data "aws_subnet_ids" "default" { | ||
vpc_id = data.aws_vpc.default.id | ||
} | ||
|
||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] | ||
} | ||
|
||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
|
||
owners = ["099720109477"] # Canonical | ||
} | ||
|
||
resource "aws_launch_configuration" "dev_server_role_dev_env" { | ||
image_id = data.aws_ami.ubuntu.id | ||
instance_type = "t2.medium" | ||
key_name = var.key_name | ||
security_groups = [aws_security_group.instance.id] | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_autoscaling_group" "dev_server_role_dev_env" { | ||
launch_configuration = aws_launch_configuration.dev_server_role_dev_env.name | ||
vpc_zone_identifier = data.aws_subnet_ids.default.ids | ||
|
||
min_size = 1 | ||
max_size = 1 | ||
|
||
tag { | ||
key = "Name" | ||
value = var.instance_name | ||
propagate_at_launch = true | ||
} | ||
} | ||
|
||
resource "aws_security_group" "instance" { | ||
name = "terraform-example-instance" | ||
ingress { | ||
from_port = 22 | ||
to_port = 22 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
description = "ingress from ssh" | ||
} | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
localhost | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
--- | ||
- hosts: localhost | ||
remote_user: root | ||
roles: | ||
- dev-server-role | ||
- hosts: all | ||
user: ubuntu | ||
become: yes | ||
gather_facts: yes | ||
|
||
vars_files: | ||
- ../defaults/main.yml | ||
|
||
tasks: | ||
- import_tasks: ../tasks/main.yml |