Skip to content

Commit

Permalink
Updates after ch1 and partial ch2 edit pass
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed May 31, 2024
1 parent f002c81 commit 2dbc307
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ch2/ansible/create_ec2_instance_playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
hosts: localhost
gather_facts: no
environment:
AWS_REGION: us-east-2
AWS_REGION: us-east-2
tasks:
- name: Create security group
amazon.aws.ec2_security_group:
Expand Down
2 changes: 1 addition & 1 deletion ch2/ansible/inventory.aws_ec2.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugin: amazon.aws.aws_ec2
regions:
- us-east-2
- us-east-2
keyed_groups:
- key: tags.Ansible
leading_separator: ''
8 changes: 2 additions & 6 deletions ch2/bash/deploy-ec2-instance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@ export AWS_DEFAULT_REGION="us-east-2"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
user_data=$(cat "$SCRIPT_DIR/../../ch/ec2-user-data-script/user-data.sh")

echo "Creating security group"
security_group_id=$(aws ec2 create-security-group \
--group-name "sample-app-script" \
--group-name "sample-app" \
--description "Allow HTTP traffic into the sample app" \
--output text \
--query GroupId)

echo "Adding rule to allow inbound requests on port 80 to security group"
aws ec2 authorize-security-group-ingress \
--group-id "$security_group_id" \
--protocol tcp \
--port 80 \
--cidr "0.0.0.0/0" > /dev/null

echo "Creating EC2 instance with Amazon Linux 2003 AMI"
instance_id=$(aws ec2 run-instances \
--image-id "ami-0900fe555666598a2" \
--instance-type "t2.micro" \
--security-group-ids "$security_group_id" \
--user-data "$user_data" \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=sample-app-script}]' \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=sample-app}]' \
--output text \
--query Instances[0].InstanceId)

echo "Getting public IP address of EC2 instance"
public_ip=$(aws ec2 describe-instances \
--instance-ids "$instance_id" \
--output text \
Expand Down
16 changes: 6 additions & 10 deletions ch2/packer/sample-app.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@ packer {
}
}

source "amazon-ebs" "amazon_linux" {
source "amazon-ebs" "amazon_linux" {
ami_name = "sample-app-packer-${uuidv4()}"
ami_description = "An Amazon Linux 2023 AMI that has a Node.js sample app installed."
ami_description = "Amazon Linux 2023 AMI with a Node.js sample app."
instance_type = "t2.micro"
region = "us-east-2"
source_ami = "ami-0900fe555666598a2"
ssh_username = "ec2-user"
}

build {
sources = [
"source.amazon-ebs.amazon_linux"
]
build {
sources = ["source.amazon-ebs.amazon_linux"]


provisioner "file" {
provisioner "file" {
source = "app.js"
destination = "/home/ec2-user/app.js"
}


provisioner "shell" {
provisioner "shell" {
inline = [
"curl -fsSL https://rpm.nodesource.com/setup_21.x | sudo bash -",
"sudo yum install -y nodejs"
Expand Down
10 changes: 5 additions & 5 deletions ch2/tofu/ec2-instance/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
provider "aws" {
provider "aws" {
region = "us-east-2"
}

resource "aws_security_group" "sample_app" {
resource "aws_security_group" "sample_app" {
name = "sample-app-tofu"
description = "Allow HTTP traffic into the sample app"
}

resource "aws_security_group_rule" "allow_http_inbound" {
resource "aws_security_group_rule" "allow_http_inbound" {
type = "ingress"
protocol = "tcp"
from_port = 8080
Expand All @@ -16,8 +16,8 @@ resource "aws_security_group_rule" "allow_http_inbound" {
cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_instance" "sample_app" {
ami = var.ami_id
resource "aws_instance" "sample_app" {
ami = var.ami_id
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.sample_app.id]
user_data = file("${path.module}/user-data.sh")
Expand Down
2 changes: 1 addition & 1 deletion ch2/tofu/ec2-instance/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "ami_id" {
description = "The ID of the AMI to run. Should be built from the Packer template in the packer folder."
description = "The ID of the AMI to run."
type = string
}

0 comments on commit 2dbc307

Please sign in to comment.