How to launch EC2 windows instance with powershell script using terraform

0 votes

Hi All,
I want to create a Windows server Ec2 instance using terraform. Once launched, then need to download URL from the S3 bucket. This URL needs some authentication to download. So when this EC2 instance download URL from the S3 bucket then it will assign username and password. I have to do this windows EC2 instance. Is this possible to do this activity with PowerShell script? If possible give me some examples to do the practice. Thanks in advance.

Jul 21, 2020 in Terraform by Lakshminarayanan
• 1,370 points

recategorized Jul 21, 2020 by MD 3,673 views

1 answer to this question.

0 votes

Hi,

In your Terraform code, you have written to launch a Windows instance. So in the same code create one s3 bucket and give public access. To download from the s3 bucket use remote execute provisioner. You can see the below code.

resource "aws_instance" "web" {
  # ...
  provisioner "remote-exec" {
    inline = [
   ]
  }
answered Jul 21, 2020 by MD
• 95,440 points

Hi @MD

Many thanks for your reply. I have already bucket there with some URLs. So I have to download this URL from the launched Windows EC2 instance. This URL should authentication like username and password. So should I write PowerShell script inside inline like below?

    inline = [
#command to download url
#command to download url
   ]

Is this right? Bro

In Terraform when we want to run any command inside an instance we use remote-exec provisioner. This command will run automatically inside that instance. Now according to your requirement you want to download something from your S3 bucket to that instance. So in your Terraform code pass the URL inside remote-exec provisioner. But regarding your username and password. I don't think terraform will ask you. It will fail. So you have to give access to your bucket so that Terraform can download without username and password.
Yes, I understand. In S3 bucket have some URL which is like oracle java. so whenever it download from browser it will ask us username and password. Same can't we do this url in terraform code?
I have created sample template to launch windows instance with remote-exec for copy file from s3 bucket. But it didnt copy anything. Below is my code. Kindly let me know if any correction.

provider "aws" {

  version = "~> 2.0"

  region  = "us-east-1"

}

resource "aws_instance" "sampleinstance" {

ami = "ami-xxxxxxxx"

instance_type = "t2.micro"

key_name = "samplekey"

tags = {

Name = "SampleEc2Instance"

}

provisioner "remote-exec" {

inline = [

"aws s3 cp s3://examplebucket/latchufile ."

]

}

}

You need to configure AWS CLI command inside your windows instance, otherwise how AWS s3 command will work.

No, I installed AWS CLI and aws configure on windows instance AMI. its custom AMI which have aws cli. bro
Then it should work. Log in to your instance via SSH. Try to do manually and see it is working or not.
Its windows EC2 instance. Manually working, but with terraform remote-exec not working. Is my code right?

provisioner "remote-exec" {

inline = [

"aws s3 cp s3://examplebucket/latchufile ."

]

}

Add destination path in your aws s3 cp command and try once.

Related Questions In Terraform

0 votes
1 answer

How to attach an EBS volume to EC2 instance using terraform?

Hi@akhtar, You can use aws_volume_attachment resource to attach ...READ MORE

answered Jun 12, 2020 in Terraform by MD
• 95,440 points
8,460 views
0 votes
1 answer

How to do ssh in EC2 instance using Terraform?

Hi@akhtar, To ssh in your EC2 instance, you ...READ MORE

answered Jun 15, 2020 in Terraform by MD
• 95,440 points
6,774 views
0 votes
1 answer

How to attach Elastic IP in EC2 instance using Terraform code?

Hi@akhtar, You can use aws_eip resource in your ...READ MORE

answered Jul 22, 2020 in Terraform by MD
• 95,440 points
8,191 views
0 votes
1 answer

How to launch one MySQL instance using Terraform code?

Hi@akhtar, You can use the aws_instance resource in Terraform. This ...READ MORE

answered Sep 9, 2020 in Terraform by MD
• 95,440 points
1,006 views
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,090 points
2,463 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,493 views
+2 votes
1 answer

How to attach an elastic load balancer with ec2 instance using Terraform?

Hi@akhtar, You can use aws_elb_attachment resource in Terraform. ...READ MORE

answered Aug 17, 2020 in Terraform by MD
• 95,440 points
2,616 views
0 votes
1 answer

How to launch one EC2 instance for WordPress using Terraform?

Hi@akhtar, You can use the aws_instance resource in Terraform. ...READ MORE

answered Sep 9, 2020 in Terraform by MD
• 95,440 points
636 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP