data "aws_partition" "current" {} resource "aws_codepipeline" "this" { name = var.name role_arn = var.service_role artifact_store { location = var.s3_bucket.s3_bucket_id type = "S3" } dynamic "stage" { for_each = try(var.stage, []) content { name = stage.value.name dynamic "action" { for_each = try(stage.value.action, []) content { name = action.value.name category = action.value.category owner = action.value.owner provider = action.value.provider version = try(action.value.version) input_artifacts = try(action.value.input_artifacts, []) output_artifacts = try(action.value.output_artifacts, []) configuration = try(action.value.configuration, {}) } } } } lifecycle { # prevents github OAuthToken from causing updates, since it's removed from state file ignore_changes = [stage[0].action[0].configuration] } tags = var.tags } # Comment this out if using AWS Event Engine resource "aws_codestarnotifications_notification_rule" "this" { name = "${var.name}_pipeline_execution_status" detail_type = "FULL" event_type_ids = [ "codepipeline-pipeline-action-execution-succeeded", "codepipeline-pipeline-action-execution-failed" ] resource = aws_codepipeline.this.arn target { address = var.sns_topic } tags = var.tags } ################################################################################ # IAM ################################################################################ data "aws_iam_policy_document" "assume_role_policy" { count = var.create_iam_role ? 1 : 0 statement { sid = "CodepipelineAssumeRole" actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["codepipeline.${data.aws_partition.current.dns_suffix}"] } } } resource "aws_iam_role" "this" { count = var.create_iam_role ? 1 : 0 name = var.iam_role_use_name_prefix ? null : var.iam_role_name name_prefix = var.iam_role_use_name_prefix ? "${var.iam_role_name}-" : null assume_role_policy = data.aws_iam_policy_document.assume_role_policy[0].json tags = var.tags } resource "aws_iam_policy" "this" { count = var.create_iam_role ? 1 : 0 name = var.iam_role_name description = "IAM Policy for Role ${var.iam_role_name}" policy = data.aws_iam_policy_document.this[0].json tags = var.tags } resource "aws_iam_role_policy_attachment" "this" { count = var.create_iam_role ? 1 : 0 policy_arn = aws_iam_policy.this[0].arn role = aws_iam_role.this[0].name } data "aws_iam_policy_document" "this" { count = var.create_iam_role ? 1 : 0 statement { sid = "AllowS3Actions" effect = "Allow" actions = [ "s3:PutObject", "s3:GetObject", "s3:GetObjectVersion", "s3:GetBucketAcl", "s3:List*" ] resources = ["${var.s3_bucket.s3_bucket_arn}/*"] } statement { sid = "AllowCodebuildActions" effect = "Allow" actions = [ "codebuild:BatchGetBuilds", "codebuild:StartBuild", "codebuild:BatchGetBuildBatches", "codebuild:StartBuildBatch", "codebuild:StopBuild" ] resources = var.code_build_projects } statement { sid = "AllowCodebuildList" effect = "Allow" actions = [ "codebuild:ListBuilds" ] resources = ["*"] } statement { sid = "AllowCodeDeployActions" effect = "Allow" actions = [ "codedeploy:CreateDeployment", "codedeploy:GetApplication", "codedeploy:GetApplicationRevision", "codedeploy:GetDeployment", "codedeploy:GetDeploymentGroup", "codedeploy:RegisterApplicationRevision" ] resources = var.code_deploy_resources } statement { sid = "AllowCodeDeployConfigs" effect = "Allow" actions = [ "codedeploy:GetDeploymentConfig", "codedeploy:CreateDeploymentConfig", "codedeploy:CreateDeploymentGroup", "codedeploy:GetDeploymentTarget", "codedeploy:StopDeployment", "codedeploy:ListApplications", "codedeploy:ListDeploymentConfigs", "codedeploy:ListDeploymentGroups", "codedeploy:Listdeployments" ] resources = ["*"] } statement { sid = "AllowCECSServiceActions" effect = "Allow" actions = [ "ecs:ListServices", "ecs:ListTasks", "ecs:DescribeServices", "ecs:DescribeTasks", "ecs:DescribeTaskDefinition", "ecs:DescribeTaskSets", "ecs:DeleteTaskSet", "ecs:DeregisterContainerInstance", "ecs:CreateTaskSet", "ecs:UpdateCapacityProvider", "ecs:PutClusterCapacityProviders", "ecs:UpdateServicePrimaryTaskSet", "ecs:RegisterTaskDefinition", "ecs:RunTask", "ecs:StartTask", "ecs:StopTask", "ecs:UpdateService", "ecs:UpdateCluster", "ecs:UpdateTaskSet" ] resources = ["*"] } statement { sid = "AllowIAMPassRole" effect = "Allow" actions = [ "iam:PassRole" ] resources = ["*"] } statement { sid = "AllowCloudWatchActions" effect = "Allow" actions = [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ] resources = ["*"] } }