In this section, we need to deploy
- Infrastructure
- Application (Server)
- Terraform CLI
- Docker
- Linux (in VM, WSL or Configure a EC2 Instance)
Note: you should configure another key-pair in
aws_instance
block. And configure ASG later, so we you destroy the infrastructure, please make you clean up ASG first.
First, you need to make a plan for deployment
terraform init
Result Logs:
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.68.0...
- Installed hashicorp/aws v5.68.0 (signed by HashiCorp)
...
Terraform has been successfully initialized!
...
Now we have to validate our configuration and plan it
terraform plan
Then tell Terraform apply the plan to deploy our infrastructure
terraform apply
Before we go to these steps, you should do something:
- Copy your SSH private key (of EC2) to
key
directory. - Copy all of Terraform outputs in the previous test.
Change directory to 000006-auto-scaling
, you need to change the environment variable in deploy.sh
script first
Copy the content of deploy-application
to the EC2 which is created.
chmod 400 key/key-name.pem
scp -i key/key-name.pem -r deploy-application ec2-user@"ec2-public-ipv4":/home/ec2-user
Result
Connect SSH to the EC2 Instance
Run the deploy.sh
script
bash deploy.sh
Note: some packages require you allowing, so you should pay attention for them.
You'll be asked for DB Password, just type it and the deployment will be continute. PLEASE, TYPE CAREFULLY!!!
In the case you type the password incorrectly, you can use the script bellow.
mysql -h $DB_HOST -u $DB_USER $DB_NAME -p < init.sql
Note: replace values for these Environment Variables.
After the deployment is done, check the result
First, you should copy values public_sg_id
, target_group_arn
, server_id
and subnet ids in Infrastructure Output. Then go to deploy-asg/main.tf
and replace corresponding values.
Change directory to deploy-asg
, and use terraform to deploy ASG like the 2 step.
terraform init
Now we have to validate our configuration and plan it
terraform plan
Then tell Terraform apply the plan to deploy our infrastructure
terraform apply
Go back to Load Balancer console, enter the LB which is created before. Go to tab Resource map - new and check
You can see the target is health. Now copy public dns of Load Balancer, paste to address bar of browser, hit enter and see the result
Change directory to deploy-asg
terraform destroy
Result Logs
aws_autoscaling_group.asg: Destruction complete after 6m34s
aws_launch_template.my_launch_template: Destroying... [id=lt-06f65e37f1e2d88a3]
aws_launch_template.my_launch_template: Destruction complete after 0s
aws_ami_from_instance.instance_ami: Destroying... [id=ami-0b8965a8bb3b921da]
aws_ami_from_instance.instance_ami: Destruction complete after 5s
Destroy complete! Resources: 3 destroyed.
Change directory to deploy-infrastructure
terraform destroy
Result Logs
aws_subnet.private_subnet_2: Destruction complete after 0s
aws_subnet.private_subnet_3: Destruction complete after 0s
aws_security_group.db_sg: Destruction complete after 0s
aws_vpc.aslab: Destroying... [id=vpc-0e9713682d8f3d6ae]
aws_vpc.aslab: Destruction complete after 1s
Destroy complete! Resources: 32 destroyed.