- Make sure you have AWS CLI
Output should be something like (there and further stdout)
$ aws --version
aws-cli/1.16.96 Python/2.7.10 Darwin/17.7.0 botocore/1.12.86
- Your credential have been configured
stdout:
cat ~/.aws/credentials[default] aws_access_key_id = AKKNKXF3KGBDQ5BFFVQ aws_secret_access_key = t72aLybwhs+PhJPSpk/c23pzKJ1DBO6HFrZFLki
- Make sure you've terraform
stdout:
terraform -v
Terraform v0.11.11
-
Go to the AWS Management Console -> Service -> EC2 (choose your region) -> instances

-
Now we need to generate a template project
mkdir terraform-dir && cd terraform-dir- Create main.tf
{
echo 'resource "aws_instance" "example" {'
echo ' # ...instance configuration...'
echo '}'
} >> main.tfterraform-dir └ main.tf
main.tf
resource "aws_instance" "example" {
# ...instance configuration...
}- Initialize terraform project
terraform initstdout:
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (1.57.0)...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 1.57"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
- terraform import
terraform import aws_instance.example i-0c3a0e151e3224520stdout:
provider.aws.region
The region where AWS operations will take place. Examples
are us-east-1, us-west-2, etc.
Default: us-east-1
Enter a value:
-Entering region:
eu-west-1stdout:
aws_instance.example: Importing from ID "i-0c3a0e151e3224520"...
aws_instance.example: Import complete!
Imported aws_instance (ID: i-0c3a0e151e3224520)
aws_instance.example: Refreshing state... (ID: i-0c3a0e151e3224520)
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
Now we have terraform.tfstate created with current configuration of imported instance. We can read it by running:
terraform showThere if we should edit main.tf according to terraform.tfstate, if we will change some configuration and run terraform plan - it will show us what changes will happen if we will apply edited main.tf configuration.