I am trying to deploy an EC2 instance on Amazon Web Services using Terraform. I have AWS CLI installed and working on a linux box. Using terraform I would like to mimic the action of the command line instruction below (though hopefully with a little bit of improvement):
aws ec2 run-instances --image-id ami-0127d62154efde733 --count 1 --instance-type t3a.nano --key-name aws-key --security-group-ids launch-wizard-13 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test}]'
This will create an instance in eu-west-1c (though this is not defined, and my account is selected as being in eu-west-1) and I can ssh in no problem.
- I'd like to have a simple .tf file to mimic the behaviour of the above command line.
- I would like to define the region where the server is being deployed, e.g. being able to spin up a server in the US would be nice.
- The image I'm wanting to use is the most recent ubuntu server, so wild carding for the image type would be preferable to using an id (I believe there may be different id's for different regions, but on that I'm not sure).
- The security group (launch-wizard-13) is defined in my account, in the Network and Security settings.
I've tried looking at the official documentation, blogs and github repositories but can't get a simple .tf file to work for the above case. Usually it's the security group that's the problem, but if I leave that section out from the command line above, then I can't ssh in. Please help.
edit:
In repsonse to @Marcin, the full .tf I'm presently running (terraform apply) is
provider "aws" {
region = "eu-west-2"
}
resource "aws_instance" "myEc2" {
ami = "ami-0127d62154efde733"
instance_type = "t3a.nano"
key_name = "aws-key"
security_groups = [
"launch-wizard-13"
]
tags = {
Name = "test"
}
}
which results in the error,
aws_instance.myEc2: Creating...
Error: Error launching instance, possible mismatch of Security Group IDs and Names. See AWS Instance docs here: https://terraform.io/docs/providers/aws/r/instance.html.
AWS Error: InvalidParameterValue: Value () for parameter groupId is invalid. The value cannot be empty
status code: 400, request id: 22b572d8-d0d3-4e2e-ba1b-3db91d2e05f6
on terraform-ec2.tf line 5, in resource "aws_instance" "myEc2":
5: resource "aws_instance" "myEc2" {