-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sudoblark/feature/initial-module-setup
Initial module setup
- Loading branch information
Showing
12 changed files
with
459 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Retrieve the current AWS Account info | ||
data "aws_caller_identity" "current_account" {} | ||
|
||
# Get current region | ||
data "aws_region" "current_region" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
locals { | ||
raw_iam_roles = [ | ||
{ | ||
suffix = "redshift-role", | ||
iam_policy_statements = [ | ||
{ | ||
sid = "AllowListGlueBuckets" | ||
effect = "Allow" | ||
actions = [ | ||
"s3:ListBucket", | ||
"s3:GetBucketAcl" | ||
] | ||
resources = [ | ||
"arn:aws:s3:::raw-bucket", | ||
"arn:aws:s3:::processed-bucket" | ||
] | ||
}, | ||
{ | ||
sid = "AllowGlueBucketObjectManipulation" | ||
effect = "Allow" | ||
actions = [ | ||
"s3:GetObject", | ||
"s3:PutObject" | ||
] | ||
resources = [ | ||
"arn:aws:s3:::raw-bucket/*", | ||
"arn:aws:s3:::processed-bucket/*" | ||
] | ||
}, | ||
{ | ||
sid = "AllowGlueTableAccess" | ||
effect = "Allow" | ||
actions = [ | ||
"glue:SearchTables", | ||
"glue:GetDatabase", | ||
"glue:GetTableVersion", | ||
"glue:GetTables", | ||
"glue:GetTableVersions", | ||
"glue:GetDatabases", | ||
"glue:GetTable", | ||
"glue:BatchGetPartition", | ||
"glue:GetPartitions" | ||
] | ||
resources = [ | ||
"arn:aws:glue:${data.aws_region.current_region.name}:${data.aws_caller_identity.current_account.account_id}:table/*/*", | ||
"arn:aws:glue:${data.aws_region.current_region.name}:${data.aws_caller_identity.current_account.account_id}:catalog", | ||
"arn:aws:glue:${data.aws_region.current_region.name}:${data.aws_caller_identity.current_account.account_id}:database/*" | ||
] | ||
}, | ||
{ | ||
sid = "AllowDescribeOwnStatements" | ||
effect = "Allow" | ||
actions = [ | ||
"redshift-data:DescribeStatement" | ||
] | ||
resources = [ | ||
"*" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
suffix = "sagemaker-executor", | ||
iam_policy_statements = [ | ||
{ | ||
sid = "AllowIAMPassRoleToSageMakerExecutionRole" | ||
effect = "Allow" | ||
actions = ["iam:PassRole"] | ||
resources = [ | ||
"arn:aws:iam::${data.aws_caller_identity.current_account.account_id}:role/aws-${var.environment}-${var.application_name}-sagemaker-executor*" | ||
] | ||
conditions : [ | ||
{ | ||
test = "StringEquals" | ||
variable = "iam:PassedToService" | ||
values = ["sagemaker.amazonaws.com"] | ||
} | ||
] | ||
}, | ||
{ | ||
sid = "AllowReadOnlyS3BucketGetObjectToSageMakerExecutionRole" | ||
effect = "Allow" | ||
actions = ["s3:GetObject"] | ||
resources = [ | ||
"arn:aws:s3:::${var.environment}-${var.application_name}-example-export-bucket/*", | ||
"arn:aws:s3::${var.environment}-${var.application_name}-example-artefact-bucket/*", | ||
"arn:aws:s3:::${var.environment}-${var.application_name}-example-output-bucket/*" | ||
] | ||
}, | ||
{ | ||
sid = "AllowReadOnlyS3ListBucketToSageMakerExecutionRole" | ||
effect = "Allow" | ||
actions = ["s3:ListBucket"] | ||
resources = [ | ||
"arn:aws:s3:::-${var.environment}-${var.application_name}-example-export-bucket", | ||
"arn:aws:s3:::-${var.environment}-${var.application_name}-example-artefact-bucket", | ||
"arn:aws:s3:::-${var.environment}-${var.application_name}-example-output-bucket" | ||
] | ||
}, | ||
{ | ||
sid = "AllowReadWriteAccessFor${title(var.environment)}S3Bucket" | ||
effect = "Allow" | ||
actions = [ | ||
"s3:PutObject", | ||
"s3:DeleteObject", | ||
"s3:AbortMultipartUpload" | ||
] | ||
resources = [ | ||
"arn:aws:s3:::${var.environment}-${var.application_name}-example-artefact-bucket/*", | ||
"arn:aws:s3:::${var.environment}-${var.application_name}-example-output-bucket/*" | ||
] | ||
}, | ||
{ | ||
sid = "AllowSageMakerAccessTo${title(var.environment)}SageMakerExecutionRole" | ||
effect = "Allow" | ||
actions = [ | ||
"sagemaker:DescribeModelPackage*", | ||
"sagemaker:ListModelPackageGroups", | ||
"sagemaker:ListModelPackages", | ||
"sagemaker:CreateModel", | ||
"sagemaker:CreatePipeline", | ||
"sagemaker:DescribePipeline", | ||
"sagemaker:CreateProcessingJob", | ||
"sagemaker:CreateTransformJob", | ||
"sagemaker:AddTags", | ||
"sagemaker:DescribeProcessingJob", | ||
"sagemaker:DescribeTransformJob", | ||
"sagemaker:DescribeModel", | ||
"sagemaker:ListPipelineExecutions", | ||
"sagemaker:ListPipelineExecutionSteps", | ||
"sagemaker:UpdatePipeline", | ||
"sagemaker:StopProcessingJob", | ||
"sagemaker:StopTransformJob" | ||
] | ||
resources = [ | ||
"arn:aws:sagemaker:${data.aws_region.current_region.name}:${data.aws_caller_identity.current_account.account_id}:*" | ||
] | ||
}, | ||
{ | ||
sid = "AllowCloudWatchAccessToSageMakerExecutionRole" | ||
effect = "Allow" | ||
actions = [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
] | ||
resources = [ | ||
"arn:aws:logs:${data.aws_region.current_region.name}:${data.aws_caller_identity.current_account.account_id}:*" | ||
] | ||
} | ||
], | ||
assume_policy_principles : [{ | ||
type = "AWS" | ||
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current_account.account_id}:role/github-actions-role"] | ||
}, | ||
{ | ||
type = "Service" | ||
identifiers = ["sagemaker.amazonaws.com"] | ||
conditions : [{ | ||
test = "StringEquals" | ||
variable = "AWS:SourceAccount" | ||
values = [data.aws_caller_identity.current_account.account_id] | ||
}] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = ">= 5.61.0" | ||
} | ||
} | ||
required_version = "~> 1.5.0" | ||
} | ||
|
||
provider "aws" { | ||
region = "eu-west-2" | ||
} | ||
|
||
module "iam_role" { | ||
source = "github.com/sudoblark/sudoblark.terraform.module.aws.iam_role?ref=1.0.0" | ||
|
||
application_name = var.application_name | ||
environment = var.environment | ||
raw_iam_roles = local.raw_iam_roles | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
variable "environment" { | ||
description = "Which environment this is being instantiated in." | ||
type = string | ||
validation { | ||
condition = contains(["dev", "test", "prod"], var.environment) | ||
error_message = "Must be either dev, test or prod" | ||
} | ||
default = "prod" | ||
} | ||
|
||
variable "application_name" { | ||
description = "Name of the application utilising the resource resource." | ||
type = string | ||
default = "demo-app" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
resource "aws_iam_policy" "policy" { | ||
for_each = { for role in var.raw_iam_roles : role.suffix => role } | ||
|
||
name = format(lower("aws-${var.environment}-${var.application_name}-%s-policy"), each.value["suffix"]) | ||
path = each.value["path"] | ||
policy = data.aws_iam_policy_document.attached_policies[each.value["suffix"]].json | ||
|
||
depends_on = [ | ||
data.aws_iam_policy_document.attached_policies | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
data "aws_iam_policy_document" "attached_policies" { | ||
for_each = { for role in var.raw_iam_roles : role.suffix => role } | ||
|
||
dynamic "statement" { | ||
for_each = each.value["iam_policy_statements"] | ||
|
||
content { | ||
sid = statement.value["sid"] | ||
actions = statement.value["actions"] | ||
resources = statement.value["resources"] | ||
|
||
dynamic "condition" { | ||
for_each = statement.value["conditions"] | ||
|
||
content { | ||
test = condition.value["test"] | ||
variable = condition.value["variable"] | ||
values = condition.value["values"] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
data "aws_iam_policy_document" "assume_policies" { | ||
for_each = { for role in var.raw_iam_roles : role.suffix => role } | ||
|
||
dynamic "statement" { | ||
for_each = length(each.value["assume_policy_principles"]) > 0 ? each.value["assume_policy_principles"] : [] | ||
|
||
content { | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = statement.value["type"] | ||
identifiers = statement.value["identifiers"] | ||
} | ||
|
||
dynamic "condition" { | ||
for_each = statement.value["conditions"] | ||
|
||
content { | ||
test = condition.value["test"] | ||
variable = condition.value["variable"] | ||
values = condition.value["values"] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.