Skip to content

Conversation

@goruha
Copy link
Member

@goruha goruha commented Jun 11, 2018

What

  • Added shell script to install tf-state module
  • Added shell script to install root-iam module

Why

  • To simplify installation of tf-state
  • To simplify installation of root-iam

@goruha goruha requested a review from osterman June 11, 2018 17:57
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

sed -i "s/backend/#backend/" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will change backend in two other places:
https://github.com/cloudposse/terraform-root-modules/blob/master/aws/tfstate-backend/main.tf#L54
https://github.com/cloudposse/terraform-root-modules/blob/master/aws/tfstate-backend/main.tf#L55

I did this before:

sed -i 's/backend          "s3"             {}/#backend          "s3"             {}/' main.tf
sed -i 's/#backend          "s3"             {}/backend          "s3"             {}/' main.tf```

but it's too fragile and could be broken if the file changes
We need to think of a better way.

echo "yes" | init-terraform


echo "Add to the Geodesic Module Dockerfile following"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to the Geodesic Module Dockerfile the following ENV vars:

aknysh
aknysh previously requested changes Jun 11, 2018
Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, a few comments


## Spaces before and after `backend` required to select right word, because `backend` appears 3 times in main.tf
sed -i "s/ backend / #backend /" main.tf
sed -i "s/ role_arn / #role_arn /" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be generally true. This is maybe true for the "root" org, but certainly not on subaccounts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roles are how we determine the account in which to provision the resources, so if this is not set, terraform won't use the appropriate account.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will need to be an arg. e.g. --disable-role-arn

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we disable the role only in the root account and only once at cold start when we provision iam and we don't have any roles yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the way I typically do something like this, is:
have two different make targets with
two different "invisible-to-terraform™" versions of the resource definitions:

  • provider-with-assume-role.tf.in
  • provider-bootstrap.tf.in

and the make targets look like:

bootstrap:
    rm -f provider.tf
    ln -s provider-bootstrap.tf.in provider.tf

default:
   rm -f provider.tf
   ln -s provider-with-assume-role.tf.in provider.tf

and the install script then looks closer to:

init-terraform
make bootstrap
terraform plan -input=false -out=install-plan &&
  terraform apply -input=false install-plan &&
  mv -v install-plan install-plan.applied

init-terraform
terraform plan

export TF_BUCKET=$(echo "yes" | terraform apply | grep -o -e "tfstate_backend_s3_bucket_id\s=\s.*" | cut -d ' ' -f 3)
Copy link
Member

@osterman osterman Jun 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever, but use terraform output instead.

export TF_BUCKET=$(terraform output -json | jq -r .tfstate_backend_s3_bucket_id.value)

echo "Add to the Geodesic Module Dockerfile following"
echo "#----------------------------------------------"
echo "ENV TF_BUCKET=\"${TF_BUCKET}\""
echo "ENV TF_BUCKET_REGION=\"${TF_BUCKET_REGION}\""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output lock table too:

terraform output -json | jq -r .tfstate_backend_dynamodb_table_id.value


sed -i "s/ #role_arn / role_arn /" main.tf

echo "Add to the Geodesic Module Dockerfile following"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the following to the Geodesic Module's Dockerfile:

#!/usr/bin/env bash

## Spaces before and after `backend` required to select right word, because `backend` appears 3 times in main.tf
sed -i "s/ backend / #backend /" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more precise.

sed -Ei 's/^(\s+backend\s+)/#\1/' main.tf

export TF_BUCKET_REGION=${TF_VAR_region}

## Spaces before and after `backend` required to select right word, because `backend` appears 3 times in main.tf
sed -i "s/ #backend / backend /" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use regex style from above.


echo "yes" | init-terraform

sed -i "s/ #role_arn / role_arn /" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use regex style from above

echo "ENV TF_BUCKET=\"${TF_BUCKET}\""
echo "ENV TF_BUCKET_REGION=\"${TF_BUCKET_REGION}\""
echo "#----------------------------------------------"
echo "And rebuild the module"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then rebuild the geodesic module.


init-terraform
terraform plan

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run terraform apply here.

#!/usr/bin/env bash

## Spaces before and after `backend` required to select right word, because `backend` appears 3 times in main.tf
sed -i "s/ role_arn / #role_arn /" main.tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous PR comments. This was not updated.

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want this script duplicated into each project. If we need that, then move it to geodesic

sed -i "s/ role_arn / #role_arn /" main.tf

init-terraform
terraform plan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest perhaps:

init-terraform
terraform plan -input=false -out=install-plan &&
  terraform apply -input=false install-plan &&
  mv install-plan install-plan.applied


## Spaces before and after `backend` required to select right word, because `backend` appears 3 times in main.tf
sed -i "s/ backend / #backend /" main.tf
sed -i "s/ role_arn / #role_arn /" main.tf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the way I typically do something like this, is:
have two different make targets with
two different "invisible-to-terraform™" versions of the resource definitions:

  • provider-with-assume-role.tf.in
  • provider-bootstrap.tf.in

and the make targets look like:

bootstrap:
    rm -f provider.tf
    ln -s provider-bootstrap.tf.in provider.tf

default:
   rm -f provider.tf
   ln -s provider-with-assume-role.tf.in provider.tf

and the install script then looks closer to:

init-terraform
make bootstrap
terraform plan -input=false -out=install-plan &&
  terraform apply -input=false install-plan &&
  mv -v install-plan install-plan.applied

goruha added 3 commits June 23, 2018 20:20
* master:
  Separate `iam` into `root-iam` and `iam` (#18)
  fix usage syntax of data.aws_availability_zones... (#16)
@osterman
Copy link
Member

Fix PR name

@goruha goruha changed the title Added install ts-state script Added install scripts Jun 26, 2018
@osterman osterman dismissed aknysh’s stale review July 2, 2018 17:15

comments addressed

@goruha goruha merged commit 3f2ec13 into master Jul 2, 2018
@goruha goruha deleted the feature-add-install-tf-state branch July 2, 2018 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants