Skip to content
This repository has been archived by the owner on Jan 30, 2021. It is now read-only.

Commit

Permalink
Allow use of existing S3 bucket in other region (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru authored Jul 2, 2019
1 parent f22de4c commit 07d134b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are

## Usage


**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases.
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-aws-kops-state-backend/releases).


This example will create a DNS zone called `us-east-1.cloudxl.net` and delegate it from the parent zone `cloudxl.net` by setting `NS` and `SOA` records in the parent zone.

It will also create an S3 bucket with the name `cp-prod-kops-state` for storing `kops` state.
Expand Down Expand Up @@ -133,6 +138,7 @@ Available targets:
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
| block_public_access_enabled | Block all public access from bucket level | string | `true` | no |
| cluster_name | Kops cluster name (e.g. `us-east-1` or `cluster-1`) | string | `us-east-1` | no |
| create_bucket | Set to `false` to use existing S3 bucket for kops state store instead of creating one. | string | `true` | no |
| delimiter | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | string | `-` | no |
| domain_enabled | A boolean that determines whether a DNS Zone for the kops domain is created | string | `true` | no |
| force_destroy | A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without errors. These objects are not recoverable | string | `false` | no |
Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
| block_public_access_enabled | Block all public access from bucket level | string | `true` | no |
| cluster_name | Kops cluster name (e.g. `us-east-1` or `cluster-1`) | string | `us-east-1` | no |
| create_bucket | Set to `false` to use existing S3 bucket for kops state store instead of creating one. | string | `true` | no |
| delimiter | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | string | `-` | no |
| domain_enabled | A boolean that determines whether a DNS Zone for the kops domain is created | string | `true` | no |
| force_destroy | A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without errors. These objects are not recoverable | string | `false` | no |
Expand Down
23 changes: 22 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
provider "aws" {
version = "~> 2.17"

alias = "s3"
region = "${var.region}"
}

data "template_file" "zone_name" {
template = "${replace(var.zone_name, "$$$$", "$")}"

Expand All @@ -10,6 +17,8 @@ data "template_file" "zone_name" {
}

locals {
create_s3_bucket = "${!(var.create_bucket == "false")}"

tags = "${
merge(
var.tags,
Expand Down Expand Up @@ -45,7 +54,17 @@ module "s3_label" {
tags = "${local.tags}"
}

data "aws_s3_bucket" "default" {
provider = "aws.s3"

count = "${local.create_s3_bucket ? 0 : 1}"
bucket = "${module.s3_label.id}"
}

resource "aws_s3_bucket" "default" {
provider = "aws.s3"

count = "${local.create_s3_bucket ? 1 : 0}"
bucket = "${module.s3_label.id}"
acl = "${var.acl}"
region = "${var.region}"
Expand All @@ -67,7 +86,9 @@ resource "aws_s3_bucket" "default" {
}

resource "aws_s3_bucket_public_access_block" "default" {
count = "${var.block_public_access_enabled == "true" ? 1 : 0}"
provider = "aws.s3"

count = "${local.create_s3_bucket && var.block_public_access_enabled == "true" ? 1 : 0}"
bucket = "${aws_s3_bucket.default.id}"

block_public_acls = true
Expand Down
10 changes: 5 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ output "zone_name" {
}

output "bucket_name" {
value = "${aws_s3_bucket.default.bucket}"
value = "${coalesce(join("",aws_s3_bucket.default.*.bucket),join("",data.aws_s3_bucket.default.*.bucket))}"
description = "S3 bucket name"
}

output "bucket_region" {
value = "${aws_s3_bucket.default.region}"
value = "${coalesce(join("",aws_s3_bucket.default.*.region),join("",data.aws_s3_bucket.default.*.region))}"
description = "S3 bucket region"
}

output "bucket_domain_name" {
value = "${aws_s3_bucket.default.bucket_domain_name}"
value = "${coalesce(join("",aws_s3_bucket.default.*.bucket_domain_name),join("",data.aws_s3_bucket.default.*.bucket_domain_name))}"
description = "S3 bucket domain name"
}

output "bucket_id" {
value = "${aws_s3_bucket.default.id}"
value = "${coalesce(join("",aws_s3_bucket.default.*.id),join("",data.aws_s3_bucket.default.*.id))}"
description = "S3 bucket ID"
}

output "bucket_arn" {
value = "${aws_s3_bucket.default.arn}"
value = "${coalesce(join("",aws_s3_bucket.default.*.arn),join("",data.aws_s3_bucket.default.*.arn))}"
description = "S3 bucket ARN"
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ variable "domain_enabled" {
default = "true"
description = "A boolean that determines whether a DNS Zone for the kops domain is created"
}

variable "create_bucket" {
type = "string"
default = "true"
description = "Set to `false` to use existing S3 bucket for kops state store instead of creating one."
}

0 comments on commit 07d134b

Please sign in to comment.