Skip to content

Conversation

@YakDriver
Copy link
Member

@YakDriver YakDriver commented Aug 4, 2025

Rollback Plan

If a change needs to be reverted, we will publish an updated version of the library.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

Description

Since these actions may be merged in bulk, extra checklist to help:

  • Rebase on Actions plumbing #43669 after it's updated, deconflicted
  • Changelog
  • PR description
  • Action file
  • Test file with terraform_data trigger
  • Test results in PR
  • website/docs/actions/ file
  • Warning at top of website docs
  • Update warning on top of website docs
  • Service package registration
  • Update code for simplified actions
  • Use region pattern (framework.WithRegionModel)
  • Add 1.14 skips for tests
  • Rerun all tests after final rebase

This PR introduces the very first Terraform Action in the AWS Provider: aws_ec2_stop_instance! 🎉

Terraform Actions are an exciting new feature in Terraform Core that allow users to model imperative, side-effecting operations—like stopping an EC2 instance—within the plan and apply lifecycle. Actions can be triggered directly via terraform invoke or automatically as part of resource lifecycle events using the new lifecycle.action_trigger block.

This first Action provides support for stopping an EC2 instance by ID. It’s especially useful for cost-saving workflows, controlled shutdowns during infrastructure updates, and testing Terraform Actions in a real-world scenario. Here's an example of how to use it:

resource "aws_instance" "test" {
  ami           = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
  instance_type = data.aws_ec2_instance_type_offering.available.instance_type

  tags = {
    Name = "example-test"
  }
}

action "aws_ec2_stop_instance" "test" {
  config {
    instance_id = aws_instance.test.id
    force       = true
  }
}

resource "terraform_data" "trigger" {
  input = "trigger"
  lifecycle {
    action_trigger {
      events  = [before_create, before_update]
      actions = [action.aws_ec2_stop_instance.test]
    }
  }
}

This will automatically stop the instance before any create or update operation on the terraform_data.trigger resource, helping you integrate safe shutdown behavior into your infrastructure workflows.

Big thanks to the Terraform Core and Plugin Framework teams for enabling this new model—we’re just getting started!

Relations

References

Output from Acceptance Testing

% make t T=TestAccEC2StopInstanceAction_ K=ec2
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.24.5 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccEC2StopInstanceAction_'  -timeout 360m -vet=off
2025/08/06 10:53:42 Creating Terraform AWS Provider (SDKv2-style)...
2025/08/06 10:53:42 Initializing Terraform AWS Provider (SDKv2-style)...
=== RUN   TestAccEC2StopInstanceAction_basic
=== PAUSE TestAccEC2StopInstanceAction_basic
=== RUN   TestAccEC2StopInstanceAction_trigger
=== PAUSE TestAccEC2StopInstanceAction_trigger
=== CONT  TestAccEC2StopInstanceAction_basic
=== CONT  TestAccEC2StopInstanceAction_trigger
=== NAME  TestAccEC2StopInstanceAction_basic
    stop_instance_action_test.go:321: Progress: Starting stop operation for EC2 instance i-00a0743004675184d...
    stop_instance_action_test.go:321: Progress: Sending stop command to EC2 instance i-00a0743004675184d...
    stop_instance_action_test.go:321: Progress: Stop command sent to EC2 instance i-00a0743004675184d, waiting for instance to stop...
    stop_instance_action_test.go:321: Progress: EC2 instance i-00a0743004675184d is currently in state 'stopping', continuing to wait for 'stopped'...
    stop_instance_action_test.go:321: Progress: EC2 instance i-00a0743004675184d is currently in state 'stopped', continuing to wait for 'stopped'...
    stop_instance_action_test.go:321: Progress: EC2 instance i-00a0743004675184d has been successfully stopped
--- PASS: TestAccEC2StopInstanceAction_basic (114.93s)
--- PASS: TestAccEC2StopInstanceAction_trigger (329.06s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ec2	334.449s

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

Community Guidelines

This comment is added to every new Pull Request to provide quick reference to how the Terraform AWS Provider is maintained. Please review the information below, and thank you for contributing to the community that keeps the provider thriving! 🚀

Voting for Prioritization

  • Please vote on this Pull Request by adding a 👍 reaction to the original post to help the community and maintainers prioritize it.
  • Please see our prioritization guide for additional information on how the maintainers handle prioritization.
  • Please do not leave +1 or other comments that do not add relevant new information or questions; they generate extra noise for others following the Pull Request and do not help prioritize the request.

Pull Request Authors

  • Review the contribution guide relating to the type of change you are making to ensure all of the necessary steps have been taken.
  • Whether or not the branch has been rebased will not impact prioritization, but doing so is always a welcome surprise.

@github-actions github-actions bot added tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. provider Pertains to the provider itself, rather than any interaction with AWS. client-connections Pertains to the AWS Client and service connections. generators Relates to code generators. prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. size/XL Managed by automation to categorize the size of a PR. labels Aug 4, 2025
@YakDriver YakDriver force-pushed the f-ec2-instance-action-2 branch from d7b5734 to 636f7cb Compare August 5, 2025 19:26
@YakDriver YakDriver changed the title First action First action: aws_ec2_stop_instance Aug 6, 2025
@YakDriver YakDriver changed the title First action: aws_ec2_stop_instance First action: aws_ec2_stop_instance Aug 6, 2025
@YakDriver YakDriver force-pushed the f-ec2-instance-action-2 branch from 9e884a9 to 2020d3a Compare August 6, 2025 18:26
@YakDriver YakDriver mentioned this pull request Aug 6, 2025
7 tasks
@YakDriver YakDriver force-pushed the f-ec2-instance-action-2 branch from 2020d3a to bbf1826 Compare August 18, 2025 21:26
@github-actions github-actions bot added the service/ec2 Issues and PRs that pertain to the ec2 service. label Aug 18, 2025
@YakDriver YakDriver force-pushed the f-ec2-instance-action-2 branch from bbf1826 to a689ba9 Compare August 18, 2025 22:34
@YakDriver YakDriver changed the title First action: aws_ec2_stop_instance New action: aws_ec2_stop_instance Aug 20, 2025
@YakDriver YakDriver mentioned this pull request Aug 20, 2025
14 tasks
@github-actions github-actions bot added the documentation Introduces or discusses updates to documentation. label Sep 10, 2025
@github-actions github-actions bot added linter Pertains to changes to or issues with the various linters. repository Repository modifications; GitHub Actions, developer docs, issue templates, codeowners, changelog. labels Sep 17, 2025
@YakDriver
Copy link
Member Author

Recent:

% make t T=TestAccEC2StopInstanceAction_ K=ec2       
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
make: Running acceptance tests on branch: 🌿 f-ec2-instance-action-2 🌿...
TF_ACC=1 go1.24.6 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccEC2StopInstanceAction_'  -timeout 360m -vet=off
2025/09/16 23:21:45 Creating Terraform AWS Provider (SDKv2-style)...
2025/09/16 23:21:45 Initializing Terraform AWS Provider (SDKv2-style)...
=== RUN   TestAccEC2StopInstanceAction_basic
=== PAUSE TestAccEC2StopInstanceAction_basic
=== RUN   TestAccEC2StopInstanceAction_trigger
=== PAUSE TestAccEC2StopInstanceAction_trigger
=== CONT  TestAccEC2StopInstanceAction_basic
=== CONT  TestAccEC2StopInstanceAction_trigger
=== NAME  TestAccEC2StopInstanceAction_basic
    ec2_stop_instance_action_test.go:321: Progress: Starting stop operation for EC2 instance i-02805387ef8ba7f52...
    ec2_stop_instance_action_test.go:321: Progress: Sending stop command to EC2 instance i-02805387ef8ba7f52...
    ec2_stop_instance_action_test.go:321: Progress: Stop command sent to EC2 instance i-02805387ef8ba7f52, waiting for instance to stop...
    ec2_stop_instance_action_test.go:321: Progress: EC2 instance i-02805387ef8ba7f52 is currently in state 'stopping', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:321: Progress: EC2 instance i-02805387ef8ba7f52 is currently in state 'stopping', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:321: Progress: EC2 instance i-02805387ef8ba7f52 has been successfully stopped
--- PASS: TestAccEC2StopInstanceAction_basic (134.99s)
--- PASS: TestAccEC2StopInstanceAction_trigger (141.83s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ec2	147.577s

@YakDriver YakDriver marked this pull request as ready for review September 17, 2025 16:10
@YakDriver YakDriver requested a review from a team as a code owner September 17, 2025 16:10
@YakDriver
Copy link
Member Author

After rebase:

% make t T=TestAccEC2StopInstanceAction_ K=ec2
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
make: Running acceptance tests on branch: 🌿 f-ec2-instance-action-2 🌿...
TF_ACC=1 go1.24.6 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccEC2StopInstanceAction_'  -timeout 360m -vet=off
2025/09/17 12:11:08 Creating Terraform AWS Provider (SDKv2-style)...
2025/09/17 12:11:08 Initializing Terraform AWS Provider (SDKv2-style)...
=== RUN   TestAccEC2StopInstanceAction_basic
=== PAUSE TestAccEC2StopInstanceAction_basic
=== RUN   TestAccEC2StopInstanceAction_trigger
=== PAUSE TestAccEC2StopInstanceAction_trigger
=== CONT  TestAccEC2StopInstanceAction_basic
=== CONT  TestAccEC2StopInstanceAction_trigger
=== NAME  TestAccEC2StopInstanceAction_basic
    ec2_stop_instance_action_test.go:328: Progress: Starting stop operation for EC2 instance i-07707afd411368dc3...
    ec2_stop_instance_action_test.go:328: Progress: Sending stop command to EC2 instance i-07707afd411368dc3...
    ec2_stop_instance_action_test.go:328: Progress: Stop command sent to EC2 instance i-07707afd411368dc3, waiting for instance to stop...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-07707afd411368dc3 is currently in state 'stopping', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-07707afd411368dc3 is currently in state 'stopped', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-07707afd411368dc3 has been successfully stopped
--- PASS: TestAccEC2StopInstanceAction_trigger (91.04s)
--- PASS: TestAccEC2StopInstanceAction_basic (114.35s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ec2	120.180s

Copy link
Member

@jar-b jar-b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

% TF_ACC_TERRAFORM_PATH=/Users/jaredbaker/go/bin/terraform make t K=ec2 T=TestAccEC2StopInstanceAction_
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
make: Running acceptance tests on branch: 🌿 f-ec2-instance-action-2 🌿...
TF_ACC=1 go1.24.6 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccEC2StopInstanceAction_'  -timeout 360m -vet=off
2025/09/17 15:46:15 Creating Terraform AWS Provider (SDKv2-style)...
2025/09/17 15:46:15 Initializing Terraform AWS Provider (SDKv2-style)...
=== RUN   TestAccEC2StopInstanceAction_basic
=== PAUSE TestAccEC2StopInstanceAction_basic
=== RUN   TestAccEC2StopInstanceAction_trigger
=== PAUSE TestAccEC2StopInstanceAction_trigger
=== CONT  TestAccEC2StopInstanceAction_basic
=== CONT  TestAccEC2StopInstanceAction_trigger
=== NAME  TestAccEC2StopInstanceAction_basic
    ec2_stop_instance_action_test.go:328: Progress: Starting stop operation for EC2 instance i-0c00d744db5856923...
    ec2_stop_instance_action_test.go:328: Progress: Sending stop command to EC2 instance i-0c00d744db5856923...
    ec2_stop_instance_action_test.go:328: Progress: Stop command sent to EC2 instance i-0c00d744db5856923, waiting for instance to stop...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-0c00d744db5856923 is currently in state 'stopping', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-0c00d744db5856923 is currently in state 'stopping', continuing to wait for 'stopped'...
    ec2_stop_instance_action_test.go:328: Progress: EC2 instance i-0c00d744db5856923 has been successfully stopped
--- PASS: TestAccEC2StopInstanceAction_basic (125.10s)
--- PASS: TestAccEC2StopInstanceAction_trigger (131.65s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/ec2        138.603s

@YakDriver YakDriver merged commit dc4be2c into main Sep 17, 2025
72 checks passed
@YakDriver YakDriver deleted the f-ec2-instance-action-2 branch September 17, 2025 20:00
@github-actions
Copy link
Contributor

Warning

This Issue has been closed, meaning that any additional comments are much easier for the maintainers to miss. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v6.14.0 milestone Sep 17, 2025
@github-actions github-actions bot removed the prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. label Sep 18, 2025
@github-actions
Copy link
Contributor

This functionality has been released in v6.14.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link
Contributor

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 Oct 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

client-connections Pertains to the AWS Client and service connections. documentation Introduces or discusses updates to documentation. generators Relates to code generators. linter Pertains to changes to or issues with the various linters. provider Pertains to the provider itself, rather than any interaction with AWS. repository Repository modifications; GitHub Actions, developer docs, issue templates, codeowners, changelog. service/ec2 Issues and PRs that pertain to the ec2 service. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants