Skip to content

Instantly share code, notes, and snippets.

@artburkart
Last active March 27, 2017 02:09
Show Gist options
  • Save artburkart/808e68c8cf39bd209966779eaa1e536c to your computer and use it in GitHub Desktop.
Save artburkart/808e68c8cf39bd209966779eaa1e536c to your computer and use it in GitHub Desktop.
Terraform #12716

Should look like this:

$ tree
.
├── production
│   ├── production.tf
│   └── terraform.tfstate
└── site
    └── main.tf

Keys in eu-west-1

$ aws ec2 --profile arthur describe-key-pairs --region eu-west-1
{
    "KeyPairs": [
        {
            "KeyName": "arthur", 
            "KeyFingerprint": ""
        }, 
        {
            "KeyName": "github", 
            "KeyFingerprint": ""
        }
    ]
}

Keys in us-east-1

$ aws ec2 --profile arthur describe-key-pairs --region us-east-1
{
    "KeyPairs": [
        {
            "KeyName": "arthur-east", 
            "KeyFingerprint": ""
        }, 
        {
            "KeyName": "packer_580427e7-bdfe-d059-c5f2-527c3c7bd805", 
            "KeyFingerprint": ""
        }
    ]
}

Operating terraform directory

$ cd production

$ AWS_PROFILE=arthur AWS_DEFAULT_REGION=us-east-1 AWS_REGION=us-east-1 terraform import module.production-eu-west-1.aws_key_pair.test arthur-east
module.production-eu-west-1.aws_key_pair.test: Importing from ID "arthur-east"...
module.production-eu-west-1.aws_key_pair.test: Import complete!
  Imported aws_key_pair (ID: arthur-east)
module.production-eu-west-1.aws_key_pair.test: Refreshing state... (ID: arthur-east)
Error importing: 1 error(s) occurred:

* module.production-eu-west-1.aws_key_pair.test (import id: arthur-east): 1 error(s) occurred:

* import module.production-eu-west-1.aws_key_pair.test result: arthur-east: import module.production-eu-west-1.aws_key_pair.test (id: arthur-east): Terraform detected a resource with this ID doesn't
exist. Please verify the ID is correct. You cannot import non-existent
resources using Terraform import.

$ AWS_PROFILE=arthur AWS_DEFAULT_REGION=us-east-1 AWS_REGION=us-east-1 terraform import module.production-eu-west-1.aws_key_pair.test arthur
module.production-eu-west-1.aws_key_pair.test: Importing from ID "arthur"...
module.production-eu-west-1.aws_key_pair.test: Import complete!
  Imported aws_key_pair (ID: arthur)
module.production-eu-west-1.aws_key_pair.test: Refreshing state... (ID: arthur)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

Test with another resource

$ AWS_PROFILE=arthur AWS_DEFAULT_REGION=us-east-1 AWS_REGION=us-east-1 terraform import mo
dule.production-eu-west-1.aws_vpc.test vpc-e025af86
module.production-eu-west-1.aws_vpc.test: Importing from ID "vpc-e025af86"...
module.production-eu-west-1.aws_vpc.test: Import complete!
  Imported aws_vpc (ID: vpc-e025af86)
module.production-eu-west-1.aws_vpc.test: Refreshing state... (ID: vpc-e025af86)
Error importing: 1 error(s) occurred:

* module.production-eu-west-1.aws_vpc.test (import id: vpc-e025af86): 1 error(s) occurred:

* import module.production-eu-west-1.aws_vpc.test result: vpc-e025af86: import module.production-eu-west-1.aws_vpc.test (id: vpc-e025af86): Terraform detected a resource with this ID doesn't
exist. Please verify the ID is correct. You cannot import non-existent
resources using Terraform import.
# Inside ./production/production.tf
module "production-eu-west-1" {
source = "../site"
region = "eu-west-1"
}
# Inside ./site/main.tf
variable "region" {
default = "eu-west-1"
}
provider "aws" {
region = "${var.region}"
}
resource "aws_key_pair" "test" {}
resource "aws_vpc" "test" {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment