Skip to content

Instantly share code, notes, and snippets.

View hendrixroa's full-sized avatar
🏠
Working from home

Hendrix Roa hendrixroa

🏠
Working from home
View GitHub Profile
@hendrixroa
hendrixroa / template_file_ecs_container_definition_task.tf
Created April 25, 2023 01:51
Terraform template file to configure dynamically the values in a JSON template definition
data "template_file" "main" {
template = file("${path.module}/task_definition.json")
vars = {
ecr_image_url = var.repo_url
name = var.app
name_index_log = lower(var.app)
prefix_logs = var.prefix_logs
region = var.region
environment = jsonencode(concat(local.main_environment, var.environment_list))
@hendrixroa
hendrixroa / ecs_task_definition.tf
Last active April 25, 2023 01:23
AWS ECS Task defintion to config containers using template variables to avoid hardcoding.
[
{
"essential": true,
"image": "${ecr_image_url}",
"name": "${name}",
"portMappings": [
{
"containerPort": ${port},
"hostPort": ${port}
}
@hendrixroa
hendrixroa / terraform_packaging_ecr_images.tf
Created March 24, 2023 15:56
Packaging ecr docker images
resource "null_resource" "docker_packaging" {
provisioner "local-exec" {
command = <<EOF
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${data.aws_caller_identity.current.account_id}.dkr.ecr.us-east-1.amazonaws.com
gradle build -p noiselesstech
docker build -t "${aws_ecr_repository.noiselesstech.repository_url}:latest" -f noiselesstech/Dockerfile .
docker push "${aws_ecr_repository.noiselesstech.repository_url}:latest"
EOF
}
@hendrixroa
hendrixroa / aws_ecr_untaggeed_policy.tf
Created March 24, 2023 15:50
AWS ECR untagged policies to remove images
resource "aws_ecr_lifecycle_policy" "default_policy" {
repository = aws_ecr_repository.noiselesstech.name
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep only the last ${var.untagged_images} untagged images.",
"selection": {
@hendrixroa
hendrixroa / aws_ecr_registry.tf
Created March 24, 2023 15:44
AWS ECR registry creation with vulnerabilities scan when a docker image is pushing
resource "aws_ecr_repository" "noiselesstech" {
name = "noiselesstech"
image_scanning_configuration {
scan_on_push = true
}
}
@hendrixroa
hendrixroa / output_lambda_result.tf
Created March 23, 2023 20:32
Output result from a lambda invocation
output "result_lambda_invocation" {
value = aws_lambda_invocation.lambda_invocation.result
}
@hendrixroa
hendrixroa / lambda_event_test_invocation.tf
Created March 23, 2023 20:30
AWS Lambda test invocation to run lambda code passing an input once the functions has been deployed.
resource "aws_lambda_invocation" "lambda_invocation" {
function_name = "NoiselesstechExample"
input = jsonencode({
hello = "world"
data = "my super data"
goodbye = "see you"
})
triggers = {
@hendrixroa
hendrixroa / basic_lambda_function.tf
Created March 23, 2023 20:27
Basic lambda function
const logger = require('pino')();
exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
const dataEvent = JSON.stringify(event);
logger.info('Display data event: '+ dataEvent);
return context.succeed({ data: dataEvent });
};
@hendrixroa
hendrixroa / lambda_module.tf
Created March 23, 2023 20:23
Lambda module definition to provision
module "lambda_noiselesstech" {
source = "../terraform-aws-lambda" // A public registry path is allowed too
code_location = "./lambdas/example"
filename = "example.zip"
lambda_function_name = "NoiselesstechExample"
lambda_runtime = "nodejs14.x"
layer_arn = module.layer.arn
environment_variables = {
@hendrixroa
hendrixroa / null_resource_packaging_nodejs.tf
Created March 23, 2023 20:20
Null resource terraform to packaging node modules given a package.json
resource "null_resource" "main" {
triggers = {
updated_at = timestamp()
}
provisioner "local-exec" {
command = <<EOF
yarn config set no-progress
yarn