DevOps Classroom Series – 09/Mar/2021

Creating ec2 instances in App and web subnets

  • Architecture: Preview
  • Lets find the ec2 resource Refer Here for the aws_instance Preview
  • Manual Steps:
    1. choose an ami. Every ami has an id and this ami id for image changes from region to region. So we need to find an approach to make an ami id work for any region Preview
    2. choose an instance type Preview
    3. Select the vpc and subnet id Preview
    4. add storage Preview
    5. Add tags Preview
    6. Select security group Preview
    7. Select a key pair Preview
  • To create ec2 instance initially, lets hardcode the details, then we will add the dynamic nature to the template
  • Refer Here for the changes done.
  • Apply the template and verify the web server created Preview Preview Preview
  • Lets try to correct the ami id and make it dynamic. So we need to fetch the information from aws, so we need a datasource. Refer Here for the documentation Preview
  • Now lets mark aws ec2 instance to be deleted and recreated, This is referred as taint. Refer Here Preview
  • Lets also create a variable for instance type Refer Here for the change set
  • Apply the template and verify the ec2 instance details Preview
  • Now lets try to create an appserver instance Refer Here for the changeset
  • Now apply the template Preview
  • the security group issue found in the class is fixed Refer Here for the changeset
  • Taint a resource will cause the resource to be recreated during next apply. once the resource is succesfully recreated then terraform marks the resource as untainted Preview
  • If you accidentally tainted resource, you can untaint
terraform untaint <resource>
terraform apply
# resource will not be recreated.
  • Next Steps:
    • We need to login into ec2 instances and deploy application.
    • We will learn about reusability in terraform
    • We will learn about multi user scenarios

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin