This demonstration is to show how to create a Codepipeline in AWS using Terraform.
We will be provisioning AWS Codepipeline and its supporting AWS Services like AWS Codbuild
and AWS CodeCommit
and AWS ECR
and finally using the provisioned codepipeline to provision lambda resource and deploy a sample lambda application ref folder: lambda_bootstrap
into AWS.
- Install Terraform : link
- Install AWS CLI : link
- Configure AWS CLI with AWS Account do
aws sts get-caller-identity
for validation) : link
.
|-- img
| |-- codepipeline-output.png
| `-- codepipeline-using-terraform.png
|-- lambda_bootstrap
| |-- lambda
| | |-- Dockerfile
| | |-- aws-lambda-url.py
| | |-- docker-test.sh
| | `-- requirements.txt
| |-- main.tf
| |-- outputs.tf
| |-- providers.tf
| |-- terraform.tfvars
| |-- variables.tf
| `-- versions.tf
|-- terraform
| |-- modules
| | |-- codecommit
| | | |-- main.tf
| | | |-- outputs.tf
| | | `-- variables.tf
| | |-- codepipeline
| | | |-- templates
| | | | |-- buildspec_build.yml
| | | | `-- buildspec_deploy.yml
| | | |-- main.tf
| | | |-- outputs.tf
| | | |-- roles.tf
| | | `-- variables.tf
| | `-- ecr
| | |-- main.tf
| | |-- outputs.tf
| | `-- variables.tf
| |-- main.tf
| |-- outputs.tf
| |-- providers.tf
| |-- terraform.tfvars
| |-- variables.tf
| `-- versions.tf
`-- README.md
-
cd create-codepipeline-using-terraform/terraform
-
run
terraform init
-
run
terraform validate
-
run
terraform plan
and review the output interminal
-
run
terraform apply
and review the output interminal
and when ready, typeyes
and hit enter -
you should be seeing output simillar to this:
Apply complete! Resources: 11 added, 0 changed, 0 destroyed. Outputs: codecommit = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/cloudcomps_devops_tf123_code_repo" codepipeline = "arn:aws:codepipeline:us-east-1:<account#>:cloudcomps_devops_tf123_dev_pipeline" ecrrepo = "<account#>.dkr.ecr.us-east-1.amazonaws.com/cloudcomps_devops_tf123_docker_repo"
-
run
cd..
into the root folder and copy thecodecommit
info from above output. -
run
git clone <https://git-codecommit.us-east-1.amazonaws.com/v1/repos/cloudcomps_devops_tf123_code_repo>
-
If credentials are required, Generate a CodeCommit credentials from
aws console
for the IAM user that you logged in:- Select
Users
from IAM (Access Management Tab) - Select the user that you want to provide CodeCommit Access to.
- Select
Security Credentials
from the User information panel. - Scroll down and you should be seeing a subsection
HTTPS Git credentials for AWS CodeCommit
- Click on
Generate Credentials
, you should be prompted with Download credentails in cvs file.
- Select
-
Once git clone and git authentication is sucessfull, cd to cloned directory
cloudcomps_devops_tf123_code_repo
-
copy lambda application code from
lambda_bootstrap
folder to git repo by runningcp -R lambda_bootstrap cloudcomps_devops_tf123_code_repo/
-
go to cloned repo
cd cloudcomps_devops_tf123_code_repo
-
push the changes to git repo by running
git add. && git commit -m "Initial Commit" && git push
-
Thats it!, you can now navigate to AWS Codepipeline from
aws console
and check the pipeline status. -
If everything goes well, you should be seeing an output simillar to this:
Reference links:
- AWS Codepipeline : https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codepipeline
- AWS CodeBuild: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codebuild_project
- AWS CodeCommit: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codecommit_repository
- AWS ECR: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository
- Terraform Output Values: https://www.terraform.io/language/values/outputs
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.