{ "Description": "This template creates a CodePipeline pipeline and a CodeBuild job.\nIt also creates the roles and policies necessary, and the buckets \nto store source and artifacts.\n", "Resources": { "PipelineRole": { "Type": "AWS::IAM::Role", "Metadata": { "Comment": "CodePipeline pipeline role" }, "Properties": { "AssumeRolePolicyDocument": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": "codepipeline.amazonaws.com" } } ], "Version": "2012-10-17" } } }, "PipelinePolicy": { "Type": "AWS::IAM::Policy", "Metadata": { "Comment": "CodePipeline pipeline policy" }, "Properties": { "PolicyName": "pipeline-policy", "Roles": [ { "Ref": "PipelineRole" } ], "PolicyDocument": { "Statement": [ { "Action": [ "s3:GetObject*", "s3:GetBucket*", "s3:List*", "s3:DeleteObject*", "s3:PutObject*", "s3:Abort*" ], "Effect": "Allow", "Resource": [ { "Fn::GetAtt": [ "ArtifactBucket", "Arn" ] }, { "Fn::Join": [ "", [ { "Fn::GetAtt": [ "ArtifactBucket", "Arn" ] }, "/*" ] ] }, { "Fn::GetAtt": [ "SourceBucket", "Arn" ] }, { "Fn::Join": [ "", [ { "Fn::GetAtt": [ "SourceBucket", "Arn" ] }, "/*" ] ] } ] }, { "Action": [ "codebuild:StartBuild", "codebuild:BatchGetBuilds", "codebuild:StopBuild", "codebuild:RetryBuild", "codebuild:StartBuildBatch", "codebuild:RetryBuildBatch", "codebuild:StopBuildBatch" ], "Effect": "Allow", "Resource": [ { "Fn::GetAtt": [ "BuildProject", "Arn" ] } ] } ] } } }, "Pipeline": { "Type": "AWS::CodePipeline::Pipeline", "Metadata": { "Comment": "A CodePipeline pipeline" }, "Properties": { "RoleArn": { "Fn::GetAtt": [ "PipelineRole", "Arn" ] }, "ArtifactStore": { "Type": "S3", "Location": { "Ref": "ArtifactBucket" } }, "Stages": [ { "Name": "Source", "Actions": [ { "Name": "S3Source", "ActionTypeId": { "Category": "Source", "Owner": "AWS", "Provider": "S3", "Version": 1 }, "Configuration": { "S3Bucket": { "Ref": "SourceBucket" }, "S3ObjectKey": "source.zip", "PollForSourceChanges": true }, "OutputArtifacts": [ { "Name": "source" } ] } ] }, { "Name": "Build", "Actions": [ { "Name": "BuildAction0", "InputArtifacts": [ { "Name": "source" } ], "ActionTypeId": { "Category": "Build", "Owner": "AWS", "Provider": "CodeBuild", "Version": 1 }, "Configuration": { "ProjectName": { "Ref": "BuildProject" }, "EnvironmentVariables": "[\n {\n \"name\": \"MY_VAR\",\n \"type\": \"PLAINTEXT\",\n \"value\": \"abc\"\n }\n]" }, "RunOrder": 1 } ] } ] } }, "BuildProjectRole": { "Type": "AWS::IAM::Role", "Metadata": { "Comment": "CodeBuild build project role" }, "Properties": { "AssumeRolePolicyDocument": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": "codebuild.amazonaws.com" } } ], "Version": "2012-10-17" } } }, "BuildProjectPolicy": { "Type": "AWS::IAM::Policy", "Metadata": { "Comment": "CodeBuild build project policy" }, "Properties": { "PolicyDocument": { "Statement": [ { "Action": [ "codebuild:StartBuild", "codebuild:BatchGetBuilds", "codebuild:StopBuild", "codebuild:RetryBuild", "codebuild:StartBuildBatch", "codebuild:RetryBuildBatch", "codebuild:StopBuildBatch" ], "Effect": "Allow", "Resource": [ { "Fn::GetAtt": [ "BuildProject", "Arn" ] } ] } ], "Version": "2012-10-17" }, "PolicyName": "build-project-policy", "Roles": [ { "Ref": "BuildProjectRole" } ] } }, "BuildProject": { "Type": "AWS::CodeBuild::Project", "Metadata": { "Comment": "CodeBuild build project" }, "Properties": { "Artifacts": { "Type": "CODEPIPELINE" }, "Environment": { "ComputeType": "BUILD_GENERAL1_LARGE", "Type": "LINUX_CONTAINER", "Image": "aws/codebuild/amazonlinux2-x86_64-standard:5.0", "EnvironmentVariables": [ { "Name": "MY_VAR", "Type": "PLAINTEXT", "Value": "xyz" } ] }, "ServiceRole": { "Fn::GetAtt": [ "BuildProjectRole", "Arn" ] }, "Source": { "Type": "CODEPIPELINE" }, "TimeoutInMinutes": 480 } }, "SourceBucket": { "Type": "AWS::S3::Bucket", "Metadata": { "Comment": "A bucket to be used as the source for the pipeline", "guard": { "SuppressedRules": [ "S3_BUCKET_DEFAULT_LOCK_ENABLED", "S3_BUCKET_REPLICATION_ENABLED", "S3_BUCKET_LOGGING_ENABLED" ] }, "checkov": { "skip": [ { "id": "CKV_AWS_18", "comment": "Not necessary for this example" } ] } }, "Properties": { "VersioningConfiguration": { "Status": "Enabled" }, "BucketEncryption": { "ServerSideEncryptionConfiguration": [ { "ServerSideEncryptionByDefault": { "SSEAlgorithm": "AES256" } } ] }, "PublicAccessBlockConfiguration": { "BlockPublicAcls": true, "BlockPublicPolicy": true, "IgnorePublicAcls": true, "RestrictPublicBuckets": true } } }, "ArtifactBucket": { "Type": "AWS::S3::Bucket", "Metadata": { "Comment": "A bucket to be used for pipeline artifacts", "guard": { "SuppressedRules": [ "S3_BUCKET_DEFAULT_LOCK_ENABLED", "S3_BUCKET_REPLICATION_ENABLED", "S3_BUCKET_LOGGING_ENABLED" ] }, "checkov": { "skip": [ { "id": "CKV_AWS_18", "comment": "Not necessary for this example" } ] } }, "Properties": { "VersioningConfiguration": { "Status": "Enabled" }, "BucketEncryption": { "ServerSideEncryptionConfiguration": [ { "ServerSideEncryptionByDefault": { "SSEAlgorithm": "AES256" } } ] }, "PublicAccessBlockConfiguration": { "BlockPublicAcls": true, "BlockPublicPolicy": true, "IgnorePublicAcls": true, "RestrictPublicBuckets": true } } } } }