Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data ecs_runtask #12096

Closed
wants to merge 2 commits into from
Closed

Add data ecs_runtask #12096

wants to merge 2 commits into from

Conversation

bertonha
Copy link

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

Closes #1703

Release note for CHANGELOG:

data aws_ecs_run_task

start a task definition overriding it, useful for executing migrations

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccXXX'

...

@ghost ghost added size/XL Managed by automation to categorize the size of a PR. needs-triage Waiting for first response or review from a maintainer. provider Pertains to the provider itself, rather than any interaction with AWS. service/ecs Issues and PRs that pertain to the ecs service. labels Feb 19, 2020
@bertonha
Copy link
Author

bertonha commented Feb 19, 2020

Hello guys that is my first contribution in GO and to terraform.

The ideas of this PR is replace this:

locals {
  some_service_migration = {
    overrides = {
      containerOverrides = [
        {
          name    = var.service_name
          command = ["python", "manage.py", "migrate", "--noinput"]
        }
      ]
    }
    network_config = {
      awsvpcConfiguration: {
        assignPublicIp = "DISABLED"
        subnets        = aws_ecs_service.some_service.network_configuration[0].subnets
        securityGroups = aws_ecs_service.some_service.network_configuration[0].security_groups
      }
    }
  }
}

resource "null_resource" "call-sometask-migrate" {
  triggers = {
    task_definition = aws_ecs_service.sometask.task_definition
  }

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOF
aws ecs run-task \
  --cluster ${aws_ecs_cluster.some.name} \
  --task-definition ${aws_ecs_task_definition.sometask.family}:${aws_ecs_task_definition.sometask.revision} \
  --launch-type FARGATE \
  --started-by "Terraform" \
  --overrides '${jsonencode(local.sometask_migration.overrides)}' \
  --network-configuration '${jsonencode(local.sometask_migration.network_config)}'
EOF

For a terraform resource:

data "aws_ecs_run_task" "call-sometask-migrate" {
    cluster         = aws_ecs_cluster.cluster.name
    task_definition = aws_ecs_task_definition.some_service.arn
    launch_type     = "FARGATE"
    started_by      = "Terraform"

    overrides {
        container_overrides {
            name    = var.service_name
            command = ["python", "manage.py", "migrate", "--noinput"]
        }
    }

    network_configuration {
        security_groups = aws_ecs_service.some_service.network_configuration[0].security_groups
        subnets         = aws_ecs_service.some_service.network_configuration[0].subnets
    }
}

I have 2 questions
1 - how I can test it?
2 - how I can make this data only be called when the parameters changes? maybe for this that needs to be a resource?

api docs
https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html

@bertonha bertonha requested a review from bflad February 21, 2020 12:19
Base automatically changed from master to main January 23, 2021 00:57
@ewbankkit ewbankkit removed the needs-triage Waiting for first response or review from a maintainer. label Aug 30, 2021
@ewbankkit
Copy link
Contributor

Closing as stale due to inactivity.

@ewbankkit ewbankkit closed this Aug 30, 2021
@Trip09
Copy link

Trip09 commented Dec 14, 2021

great work @bertonha , it's a shame that there wasn't a review on this PR to allow merging the new resource @zhelding can you help? thanks

@aitorres
Copy link

+1. I think it would be really useful to re-open and consider this PR.

@mattheidelbaugh
Copy link

Would also really love to reopen this PR. Seems like a lot of people would fine this useful

digitalronin added a commit to digitalronin/aws-ecs-terraform that referenced this pull request May 16, 2022
There is no "clean" way to run a one-off ECS task via terraform, until a
PR like [this
one](hashicorp/terraform-provider-aws#12096) is
merged.

This change adds a "rails db:migrate" task, along with a (commented-out)
service. Uncommenting the service and running `terraform apply` will
cause ECS to start running this task.

> The task will run multiple times, so any task you run using this
method **must** be idempotent.

Once you have confirmed that the task has run successfully, comment out
the service and re-run `terraform apply` so that ECS stops launching it.

This sucks, and there are [cleaner ways to achieve
this](hashicorp/terraform-provider-aws#1703 (comment))
but this was the easiest to get running, and I've spent enough time
shaving this particular yak.
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
provider Pertains to the provider itself, rather than any interaction with AWS. service/ecs Issues and PRs that pertain to the ecs service. size/XL Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: aws_ecs_runtask
5 participants