Browse Source

unique_id used for naming

Fred Damstra (Macbook 2015) 2 years ago
parent
commit
73c951ab3a
4 changed files with 17 additions and 26 deletions
  1. 4 3
      config.tf
  2. 8 8
      iam.tf
  3. 1 3
      security_groups.tf
  4. 4 12
      vpc.tf

+ 4 - 3
config.tf

@@ -5,9 +5,10 @@ locals {
   # Everything here should be self-explanatory
   profile = "default"
   region  = "us-east-2"
-  #  tags = {
-  #    "tf_module" : basename(path.root)
-  #  }
+  tags = {
+    "tf_module" : basename(abspath(".")),
+    "project" : "monkeybox_emr_lab"
+  }
 }
 
 # Uncomment if needed

+ 8 - 8
iam.tf

@@ -4,7 +4,7 @@
 
 # IAM role for EMR Service
 resource "aws_iam_role" "iam_emr_service_role" {
-  name = "iam_emr_service_role"
+  name_prefix = local.unique_id
 
   assume_role_policy = <<EOF
 {
@@ -86,15 +86,15 @@ data "aws_iam_policy_document" "iam_emr_service_policy" {
 }
 
 resource "aws_iam_role_policy" "iam_emr_service_policy" {
-  name = "iam_emr_service_policy"
-  role = aws_iam_role.iam_emr_service_role.id
+  name_prefix = local.unique_id
+  role        = aws_iam_role.iam_emr_service_role.id
 
   policy = data.aws_iam_policy_document.iam_emr_service_policy.json
 }
 
 # IAM Role for EC2 Instance Profile
 resource "aws_iam_role" "iam_emr_profile_role" {
-  name = "iam_emr_profile_role"
+  name_prefix = local.unique_id
 
   assume_role_policy = <<EOF
 {
@@ -114,8 +114,8 @@ EOF
 }
 
 resource "aws_iam_instance_profile" "emr_profile" {
-  name = "emr_profile"
-  role = aws_iam_role.iam_emr_profile_role.name
+  name_prefix = local.unique_id
+  role        = aws_iam_role.iam_emr_profile_role.name
 }
 
 data "aws_iam_policy_document" "iam_emr_profile_policy" {
@@ -152,8 +152,8 @@ data "aws_iam_policy_document" "iam_emr_profile_policy" {
 }
 
 resource "aws_iam_role_policy" "iam_emr_profile_policy" {
-  name = "iam_emr_profile_policy"
-  role = aws_iam_role.iam_emr_profile_role.id
+  name_prefix = local.unique_id
+  role        = aws_iam_role.iam_emr_profile_role.id
 
   policy = data.aws_iam_policy_document.iam_emr_profile_policy.json
 }

+ 1 - 3
security_groups.tf

@@ -29,7 +29,5 @@ resource "aws_security_group" "allow_access" {
     ]
   }
 
-  tags = {
-    project = "monkeybox_emr_lab"
-  }
+  tags = local.tags
 }

+ 4 - 12
vpc.tf

@@ -2,13 +2,10 @@ resource "aws_vpc" "main" {
   cidr_block           = "172.16.0.0/16"
   enable_dns_hostnames = true
 
-  tags = {
-    name    = "monkeybox_emr_lab"
-    project = "monkeybox_emr_lab"
-  }
+  tags = local.tags
 }
 
-resource "aws_vpc_endpoint_route_table_association" "example" {
+resource "aws_vpc_endpoint_route_table_association" "main" {
   route_table_id  = aws_route_table.r.id
   vpc_endpoint_id = aws_vpc_endpoint.s3.id
 }
@@ -19,10 +16,7 @@ resource "aws_subnet" "main" {
   # tfsec:ignore:aws-ec2-no-public-ip-subnet We allow public IPs in the lab
   map_public_ip_on_launch = true
 
-  tags = {
-    name    = "monkeybox_emr_lab"
-    project = "monkeybox_emr_lab"
-  }
+  tags = local.tags
 }
 
 resource "aws_internet_gateway" "gw" {
@@ -33,9 +27,7 @@ resource "aws_vpc_endpoint" "s3" {
   vpc_id       = aws_vpc.main.id
   service_name = "com.amazonaws.us-east-2.s3"
 
-  tags = {
-    project = "monkeybox_emr_lab"
-  }
+  tags = local.tags
 }
 
 resource "aws_route_table" "r" {