The image repository module that you are going to implement has the following folder structure:
image-repository/
│
├── main.tf
│
├── outputs.tf
│
├── providers.tf
│
├── state.tf
│
├── terraform.tfvars
│
├── variables.tf
│
└── versions.tf
You then start building the configuration files required for the image repository module and triggering the first Terraform Cloud run for the module manually.
1. Do the following instructions to create Terraform configurations for the module.
Fill the following lines of code to image-repository/versions.tf:
terraform {
required_version = ">= 1.0.0, < 2.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.5"
}
}
}
Fill the following lines of code to image-repository/state.tf:
terraform {
cloud {
organization = "aws-first-cloud-journey"
workspaces {
name = "dev-image-repository"
project = "workshop-1"
}
}
}
Fill the following lines of code to image-repository/variables.tf:
variable "region" {
description = "The AWS region of the project"
type = string
}
variable "environment" {
description = "The environment to which the project delploys"
type = string
default = "dev"
}
variable "project_name" {
description = "The name of the project"
type = string
}
variable "force_delete" {
description = "If true, delete the repository even if it contains images"
type = bool
default = false
}
Fill the following lines of code to image-repository/terraform.tfvars:
region = "us-east-1"
environment = "dev"
project_name = "workshop-1"
# Disable in real usage
force_delete = true
Fill the following lines of code to image-repository/providers.tf:
provider "aws" {
region = var.region
}
Fill the following lines of code to image-repository/main.tf. Remember to replace <your-github-username> with your GitHub username:
module "image_repository" {
source = "git::https://github.com/<your-github-username>/workshop-1-tf-modules.git//modules/image-repository?ref=v1.0.0"
environment = var.environment
project_name = var.project_name
force_delete = var.force_delete
}
Fill the following lines of code to image-repository/outputs.tf:
output "repository_name" {
description = "The name of the repository"
value = module.image_repository.repository_name
}
output "repository_url" {
description = "The url of the repository"
value = module.image_repository.repository_url
}
output "registry_id" {
description = "The registry ID where the repository was created"
value = module.image_repository.registry_id
}
output "repository_arn" {
description = "The arn of the repo"
value = module.image_repository.repository_arn
}
2. Commit and push the module to the GitHub repository.
git add . && \
git commit -m "add image repository module" && \
git push
3. Navigate to your dev-image-repository Terraform Cloud workspace interface. Click Settings.
4. Scroll down to Remote state sharing section. Select Share with specific workspaces. Click the Select workspaces to share with dropdown and choose dev-app. Click Save settings.
5. Back to dev-image-repository Terraform Cloud workspace interface. Click New run.
6. Click Start.
7. Wait until the plan is finished. After that, review the plan.
8. If everything is fine, scroll down to the bottom and click Confirm & apply.
9. Optionally, add a comment Look good to me!
. Click Confirm plan, Terraform will run apply and provision AWS resources for you.
10. After Terraform has done the applying process, you may access your AWS account to view the Terraform-provided AWS resources.
11. Go to AWS ECR console.
12. In the left sidebar, click Repositories under the Private registry dropdown to check out your newly created repository.
13. Turn on your Docker Desktop. You next push a small Docker image that print the IP address for testing the app tier.
14. Clone this repository.
git clone https://github.com/Definitely-not-AWS-Workshops/ip-printer.git
15. Move inside the local repository you have just cloned.
cd ip-printer
16. Go to your AWS hompage. Click the AWS username dropdown and then get your AWS account ID.
17. Retrieve an authentication token and authenticate your Docker client to your registry. Replace <your-aws-account-id> with your AWS account ID from step 16.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <your-aws-account-id>.dkr.ecr.us-east-1.amazonaws.com
18. Build your Docker image using the following command.
docker build -t workshop-1 .
19. After the build completes, tag your image so you can push the image to this repository. Replace <your-aws-account-id> with your AWS account ID from step 16.
docker tag workshop-1:latest <your-aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/workshop-1:v0.0.0
20. Run the following command to push this image to your newly created AWS ECR repository. Replace <your-aws-account-id> with your AWS account ID from step 16.
docker push <your-aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/workshop-1:v0.0.0
21. Go to AWS ECR console.
22. In the left sidebar, click Repositories under the Private registry dropdown. Click the workshop-1 image repository.
23. Click Images. You successfully push the first image to the AWS ECR repository.