DevOps(Day-86): Create an EC2 instance using terraform

 

  • Task 2: Create an EC2 instance

  • Task 3: Access your website

  • Now you can create an EC2 instance with Terraform. Follow these steps:

  • In your main.tf file, add the following code to create an EC2 instance:

resource "aws_instance" "web_server" {
  ami           = "ami-0557a15b87f6559cf"
  instance_type = "t2.micro"
  key_name      = "my-key-pair"
  security_groups = [
    aws_security_group.web_server.name
  ]

  user_data = <<-EOF
              #!/bin/bash
              echo "<html><body><h1>Welcome to my website!</h1></body></html>" > index.html
              nohup python -m SimpleHTTPServer 80 &
              EOF
}

Note: Replace the ami and key_name values with your own. You can find a list of available AMIs in the AWS documentation.

Run terraform apply to create the EC2 instance.

  • Now that your EC2 instance is up and running, you can access the website you just hosted on it. Follow these steps:

Thanks for reading my article. Have a nice day.

Comments

Popular posts from this blog

DevOps(Day-97): Meta-Arguments in Terraform

DevOps(Day-95): Auto Scaling with Terraform

DevOps (Day-1) Introduction