Use GitHub Actions and Terraform to provision EC2 instance
In this tutorial, I will create simple and practical example of how to provision EC2 instance with Github Actions and Terraform. I will use workflow_dispatch type of workflow which is triggered manually by user, using Terraform Github action from HashiCorp.
This tutorial, will be first in series of tutorials for Github Actions, in next tutorials I will show how to create and use self-hosted GitHub runners with EC2, docker containers running on Kubernetes and much more.
I will try to be practical as much as possible in my tutorials, so there will not be much theory.
Goal
Deploy EC2 instance of t3.small size to your AWS account using Github Actions and Terraform.
I will use ‘workflow_dispatch’ event for this, which is manually triggered.
workflow_dispatch
This event occurs when someone triggers a workflow run on GitHub or sends a POST request to the “Create a workflow dispatch event” endpoint. For more information, see “Events that trigger workflows.”
Github Actions published guides:
-
GitOps way with Github Actions and self-hosted runner on Kubernetes
-
Automatic scaling with Github Actions and self-hosted runners
-
Github Actions with k8s and Karpenter to dynamically provision your runners on spot instances
-
Use OpenID Connect with Github Actions to authenticate with Amazon Web Services
Let’s do it
Generate AWS Credentials for terraform user
Go to your AWS account -> IAM -> Add new user
We need only ‘Programmatic access’
To simplify this tutorial, I will use EC2 Full Access permissions.
Download and save Access key id and Secret access key which will be added as secrets to github repo.
Next step will be to add credentials to Github as secrets
Go to your github repository -> settings -> secrets -> actions
My repo is https://github.com/warolv/github-actions-series, with all the code and the github actions workflow.
Add new repository secret
Need to add two secrets: TF_USER_AWS_KEY (Access key id) and TF_USER_AWS_SECRET (Secret access key) which will be used later in worflow I build.
Add TF_USER_AWS_SECRET in the same way.
Overview of terraform script to provision EC2 instance
- The provisioned instance based on ubuntu AMI.
- Instance type is t3.micro.
-
Instance will be provisioned to default VPC subnet security group. - Need to create SSH key pair to connect with provisioned instance.
- EC2 will be provisioned to ‘us-east-2’ region.
Generate SSH key pair to connect with EC2 instance
AWS console -> EC2 -> Key Pair -> Create Key Pair
Download .pem file, it will be used later to connect with EC2 instance.
Terraform script to provision EC2 instance (app_server)
https://github.com/warolv/github-actions-series/blob/master/tf-example/main.tf
main.tf
variables.tf
The EC2 name will be set through workflow using the inputs.
Create workflow with Github Actions to provision EC2 instance
-
‘configure-aws-credentials’ action used to set AWS credentials with docker container to be used by Terraform.
-
You need to define name of EC2 instance using terraform variables: TF_VAR_ec2_name, before TF runs.
-
Used setup-terraform action from HashiCorp.
-
Used GitHub-hosted runner: ubuntu-latest.
https://github.com/warolv/github-actions-series/blob/master/.github/workflows/provision_ec2.yaml
To see your workflow in actions -> workflows, first need to create ‘.github/workflows/provision_ec2.yaml’ and add it to your repository.
.github/workflows/provision_ec2.yaml
I am using ‘workflow_dispatch’ type of workflow which is triggered manually.
Click ‘Run workflow’
You can see using AWS console that EC2 instance created!
Now try to connect to this instance using ‘app-ssh-key’
I downloaded app-ssh-key.pem file in my mac to ~/Downloads folder
Success!
In this tutorial, I explained how to provision EC2 instance using Terraform and Github Actions workflow, I used GitHub-hosted runner and workflow_dispatch type of workflow which is triggered manually.
Thank you for reading, I hope you enjoyed it, see you in the next post.
If you want to be notified when the next post of this tutorial is published, please follow me on Twitter @warolv.
I also will create Youtube video for this tutorial on my YT channel, please subscribe!
You can get all tutorials of Gihub Action from my github repo by cloning it: ‘git clone https://github.com/warolv/github-actions-series.git‘