https://github.com/randomtask2000/terraform-codebuild-ec2-instance
Terraform CodeBuild project that builds an EC2 instance with Terraform. It essentially builds a CodeBuild project that becomes a Terraform builder.
https://github.com/randomtask2000/terraform-codebuild-ec2-instance
Last synced: 7 months ago
JSON representation
Terraform CodeBuild project that builds an EC2 instance with Terraform. It essentially builds a CodeBuild project that becomes a Terraform builder.
- Host: GitHub
- URL: https://github.com/randomtask2000/terraform-codebuild-ec2-instance
- Owner: randomtask2000
- License: mit
- Created: 2018-12-23T06:42:32.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-02T05:17:14.000Z (about 7 years ago)
- Last Synced: 2025-06-03T00:19:26.032Z (8 months ago)
- Language: HCL
- Homepage:
- Size: 1.78 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Use Terraform to build a CodeBuild project that runs terraform as a build service
This little repo illustrates how to build a `CodeBuild` project with `terraform` and run terraform inside of this project to build infrastructure from another [repo](https://github.com/randomtask2000/terraform_ec2_instance). This other repo builds a simple `ec2` instance.
## Config
Before running this terraform template add the following terraform config file.
Create a settings file `terraform.auto.tfvars` with the following:
```
echo <<< EOL
aws_access_key = "XXXXXXXXXXXXXXXXXXXX"
aws_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
aws_region = "us-east-1"
public_key = "ssh-rsa AAAAB3NzaC1yc....9wrf+M7Q== my@laptop.local"
vpc_id = "vpc-00000000x00x0xxx0"
terraform_version = "0.9.9"
s3_bucket = "your-s3-bucket-terraform-state"
debug = "true"
EOL >> terraform.auto.tfvars;
```
## terraform apply
After you're done creating the above file and adding your `aws access key`, `secret` and your `ssh public key`, run the following:
```
terraform init
terraform plan
echo yes | terraform apply
```
To remove what you built run:
```
echo yes | terraform destroy
```
The above `terraform apply` statement will create your CodeBuild project. After creating your project in AWS you should find your CodeBase project here:
https://console.aws.amazon.com/codesuite/codebuild/projects?region=us-east-1
(Change your region in this URL to what you set your `aws_region` variable in your `tfvars` file to.)
## This bit is manual in AWS Console
Your project in CodeBuild will look something like the image below:

Select your project and click `Start Build`: 
Select `Advanced build overrides`: 
Unfold `Additional configuration` and edit any of the environment variables. Set the `DESTROY` variable to `true` if you would like to run a build cycle that destroys your infrastructure. 
Press `Start Build`.
# And Bob's your uncle
You're done and have fun!