terraform { required_providers { mycloud = { source = "hashicorp/aws" version = "~> 3.0" } } } provider "aws" { profile = "default" } data "aws_caller_identity" "current" {} data "aws_region" "current" {} # Inputs variable "source_repo_name" { description = "Source repo name" type = string } variable "source_repo_branch" { description = "Source repo branch" type = string } variable "image_repo_name" { description = "Image repo name" type = string } # Outputs output "source_repo_clone_url_http" { value = aws_codecommit_repository.source_repo.clone_url_http } output "image_repo_url" { value = aws_ecr_repository.image_repo.repository_url } output "image_repo_arn" { value = aws_ecr_repository.image_repo.arn } output "pipeline_url" { value = "https://console.aws.amazon.com/codepipeline/home?region=${data.aws_region.current.name}#/view/${aws_codepipeline.pipeline.id}" } # Resources # Code Commit repo resource "aws_codecommit_repository" "source_repo" { repository_name = var.source_repo_name description = "This is the SimpleService app source repository" } # Trigger role and event rule to trigger pipeline resource "aws_iam_role" "trigger_role" { assume_role_policy = < imagedefinitions.json artifacts: files: imagedefinitions.json BUILDSPEC } } # CodePipeline resource "aws_codepipeline" "pipeline" { depends_on = [ aws_codebuild_project.codebuild ] name = "${var.source_repo_name}-${var.source_repo_branch}-Pipeline" role_arn = aws_iam_role.codepipeline_role.arn artifact_store { location = aws_s3_bucket.artifact_bucket.bucket type = "S3" } stage { name = "Source" action { name = "Source" category = "Source" owner = "AWS" version = "1" provider = "CodeCommit" output_artifacts = ["SourceOutput"] run_order = 1 configuration = { RepositoryName = var.source_repo_name BranchName = var.source_repo_branch PollForSourceChanges = "false" } } } stage { name = "Build" action { name = "Build" category = "Build" owner = "AWS" version = "1" provider = "CodeBuild" input_artifacts = ["SourceOutput"] output_artifacts = ["BuildOutput"] run_order = 1 configuration = { ProjectName = aws_codebuild_project.codebuild.id } } } }