Posts

Showing posts from March, 2019

Terraform SSH connection to AWS EC2

Image
The instances are created via Terraform, but Terraform can only used existing key pairs. First thing is to create the key pair as explained in the AWS documentation 1 . NOTE: However, if you are not familiar with Terraform, please go through the following blogs before this blog Basic example of creating AWS EC2 with Terraform Creating AWS S3 bucket with Terraform Connect to the EC2 instance, the most important thing is key value pair which is create in the time of the EC2 instance is created. But this is not currently possible in the Terraform. Therefore we need to create the mykey first. For example aws ec2 create-key-pair --key-name mykey --query 'KeyMaterial' --output text > ~/.aws/mykey.pem If you are connecting from the Linux or Mac, because need read permission. chmod 400 ~/.aws/mykey.pem To display aws ec2 describe-key-pairs --key-name mykey To retrieve the public key from the pem file (Optional) ssh-keygen -y -f ~/.aws/mykey.pem Before th

Creating AWS S3 bucket with Terraform

I recommend to read the first example 1 ,because this is a extenstion to that. However, instead of AWS EC2, here the target resource is AWS S3 for the simplicity. Here the providers.tf file. provider "aws" { region = "${var.s3_region}" } terraform { required_version = ">= 0.11.13" backend "s3" { bucket = "ojitha" key = "test/backbone" region = "ap-southeast-2" encrypt = "true" } } As shown in the above, the stage is maintain in the S3 bucket instead of locally as specified in the line# 7. resource "aws_s3_bucket" "main" { bucket = "${var.s3_bucket_prefix}-${var.environment}-${var.s3_region}" acl = "private" tags = "${local.s3_tags}" region = "${var.s3_region}" lifecycle { prevent_destroy = "false" } server_side_encryption_configuration { rule {

Basic example of creating AWS EC2 with Terraform

Here the very basic example. This is just a note of creating single EC2 instance using Terraform. First you need to define the provider in the example.tf file provider "aws" { region = "ap-southeast-2" } # resource "aws_s3_bucket" "example" { # bucket = "ojithatest1" # acl = "private" # } resource "aws_instance" "example" { ami = "${lookup(var.amis, "ubuntu-server")}" instance_type = "t2.micro" # depends_on = ["aws_s3_bucket.example"] provisioner "local-exec" { command = "echo ${aws_instance.example.public_ip} > ip_address.txt" } } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } If you need S3 bucket depends on that EC2 uncomment the above code. In the above code, we are just creating EC2 instance and the assciated Elastic IP address. Above code use the variables