Skip to content

Commit

Permalink
Update the Getting Started guide
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Apr 6, 2022
1 parent acad849 commit 997e8ec
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
79 changes: 72 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ resource "aws_s3_bucket" "example" {
}
```

Apply it and create the `aws_s3_bucket` resource with the AWS provider v3.74.3, which is the last version without deprecated warnings:
Apply it and create the `aws_s3_bucket` resource with the AWS provider v3.74.3, which is the last version without deprecation warnings:

```
# terraform -v
Expand All @@ -132,9 +132,10 @@ on linux_amd64
# terraform apply -auto-approve
# terraform state list
aws_s3_bucket.example
```

Then, let's upgrade the AWS provider to the latest v3.x, which allows you to refactor the `aws_s3_bucket` resource before upgrading v4:
Then, let's upgrade the AWS provider to the latest v3.x, which allows you to refactor the `aws_s3_bucket` resource before upgrading v4. To update the provider version constraint, of course you can edit the `required_providers` block in the `config.tf` with your text editor, but it's easy to do with [tfupdate](https://github.com/minamijoyo/tfupdate):

```
# tfupdate provider aws -v "~> 3.75" .
Expand All @@ -147,7 +148,23 @@ on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.75.1
```

Now it's time to upgrade Terraform configuration to the AWS provider v4 compatible with `tfedit`:
You can see a deprecation warning as follows:

```
# terraform validate
│ Warning: Argument is deprecated
│ with aws_s3_bucket.example,
│ on main.tf line 3, in resource "aws_s3_bucket" "example":
│ 3: acl = "private"
│ Use the aws_s3_bucket_acl resource instead
Success! The configuration is valid, but there were some validation warnings as shown above.
```

Now, it's time to upgrade Terraform configuration to the AWS provider v4 compatible with `tfedit`:

```
# tfedit filter awsv4upgrade -u -f main.tf
Expand All @@ -167,6 +184,13 @@ resource "aws_s3_bucket_acl" "example" {
}
```

You can also see that the deprecation warning has been resolved:

```
# terraform validate
Success! The configuration is valid.
```

At this point, if you run the `terraform plan` command, you can see that a new `aws_s3_bucket_acl` resource will be created:

```
Expand All @@ -175,7 +199,7 @@ At this point, if you run the `terraform plan` command, you can see that a new `
Plan: 1 to add, 0 to change, 0 to destroy.
```

Now it's time for tfmigrate, which allows you to run the `terraform import` command in a declarative way. Currently, generating a migration file feature has not been implemented yet, so create a migration file manually.
To resolve the conflict between the configuration and the existing state, you need to import the new resource. As you know, you can run the `terraform import` command directly, but if you prefer to check the upgrade results without updating remote state, use [tfmigrate](https://github.com/minamijoyo/tfmigrate), which allows you to run the `terraform import` command in a declarative way. Currently, generating a migration file feature has not been implemented yet, so you need to create a migration file manually.

```
# cat << EOF > tfmigrate_test.hcl
Expand All @@ -187,7 +211,7 @@ migration "state" "test" {
EOF
```

Run `tfmigrate plan` to check to see if `terraform plan` has no changes after the migration without updating remote tfstate:
Run the `tfmigrate plan` command to check to see if the `terraform plan` command has no changes after the migration without updating remote state:

```
# tfmigrate plan tfmigrate_test.hcl
Expand All @@ -208,9 +232,9 @@ YYYY/MM/DD hh:mm:ss [INFO] [migrator] state migrator apply success!
```

The apply command computes a new state and pushes it to remote state.
It will fail if terraform plan detects any diffs with the new state.
It will fail if the `terraform plan` command detects any diffs with the new state.

You can confirm the latest remote state has no changes with terraform plan:
You can confirm the latest remote state has no changes with the `terraform plan` command:

```
# terraform plan
Expand All @@ -222,6 +246,47 @@ aws_s3_bucket.example
aws_s3_bucket_acl.example
```

Finally, let's upgrade the AWS provider to the latest v4.x:

```
# tfupdate provider aws -v "~> 4.0" .
# terraform init -upgrade
# terraform -v
Terraform v1.1.7
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v4.8.0
# terraform validate
│ Warning: Argument is deprecated
│ with provider["registry.terraform.io/hashicorp/aws"],
│ on config.tf line 36, in provider "aws":
│ 36: s3_force_path_style = true
│ Use s3_use_path_style instead.
```

You will get the `s3_force_path_style` warning, but this is an issue caused by the sandbox environment using `localstack`, so it's ok to ignore it. (To resolve the warning, use `s3_use_path_style` instead, but note that this option is not available in v3.)

You can confirm that the result of the `terraform plan` command is no changes in v4:

```
# terraform plan
(snip.)
No changes. Infrastructure is up-to-date.
```

To clean up the sandbox environment:

```
# terraform destroy -auto-approve
# cd ../../
# rm -rf tmp/dir1
# exit
$ docker-compose down
```

## Install

### Source
Expand Down
7 changes: 4 additions & 3 deletions test-fixtures/awsv4upgrade/aws_s3_bucket/simple/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
bucket = "tfstate-test"
key = "test/terraform.tfstate"

// mock s3 endpoint with localstack
# mock s3 endpoint with localstack
endpoint = "http://localstack:4566"
access_key = "dummy"
secret_key = "dummy"
Expand All @@ -23,7 +23,7 @@ terraform {
}

# https://www.terraform.io/docs/providers/aws/index.html
# https://www.terraform.io/docs/providers/aws/guides/custom-service-endpoints.html#localstack
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/custom-service-endpoints#localstack
provider "aws" {
region = "ap-northeast-1"

Expand All @@ -34,8 +34,9 @@ provider "aws" {
skip_region_validation = true
skip_requesting_account_id = true
s3_force_path_style = true
# s3_use_path_style = true

// mock endpoints with localstack
# mock endpoints with localstack
endpoints {
s3 = "http://localstack:4566"
}
Expand Down

0 comments on commit 997e8ec

Please sign in to comment.