@@ -19,7 +19,7 @@ import (
1919 "github.com/hashicorp/terraform-provider-aws/names"
2020)
2121
22- func TestAccEC2StopInstanceAction_force (t * testing.T ) {
22+ func TestAccEC2StopInstanceAction_basic (t * testing.T ) {
2323 ctx := acctest .Context (t )
2424 var v awstypes.Instance
2525 resourceName := "aws_instance.test"
@@ -61,6 +61,31 @@ func TestAccEC2StopInstanceAction_force(t *testing.T) {
6161 })
6262}
6363
64+ func TestAccEC2StopInstanceAction_trigger (t * testing.T ) {
65+ ctx := acctest .Context (t )
66+ var v awstypes.Instance
67+ resourceName := "aws_instance.test"
68+ rName := acctest .RandomWithPrefix (t , acctest .ResourcePrefix )
69+
70+ resource .ParallelTest (t , resource.TestCase {
71+ PreCheck : func () {
72+ acctest .PreCheck (ctx , t )
73+ acctest .PreCheckPartitionHasService (t , names .EC2 )
74+ },
75+ ErrorCheck : acctest .ErrorCheck (t , names .EC2ServiceID ),
76+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories ,
77+ Steps : []resource.TestStep {
78+ {
79+ Config : testAccStopInstanceActionConfig_trigger (rName ),
80+ Check : resource .ComposeTestCheckFunc (
81+ testAccCheckInstanceExistsLocal (ctx , resourceName , & v ),
82+ testAccCheckInstanceState (ctx , resourceName , awstypes .InstanceStateNameStopped ),
83+ ),
84+ },
85+ },
86+ })
87+ }
88+
6489// testAccCheckInstanceExistsLocal checks that an instance exists and populates the instance variable
6590func testAccCheckInstanceExistsLocal (ctx context.Context , n string , v * awstypes.Instance ) resource.TestCheckFunc {
6691 return func (s * terraform.State ) error {
@@ -137,6 +162,40 @@ action "aws_ec2_stop_instance" "test" {
137162` , rName ))
138163}
139164
165+ func testAccStopInstanceActionConfig_trigger (rName string ) string {
166+ return acctest .ConfigCompose (
167+ acctest .ConfigLatestAmazonLinux2HVMEBSX8664AMI (),
168+ acctest .ConfigAvailableAZsNoOptIn (),
169+ acctest .AvailableEC2InstanceTypeForAvailabilityZone ("data.aws_availability_zones.available.names[0]" , "t3.micro" , "t2.micro" ),
170+ fmt .Sprintf (`
171+ resource "aws_instance" "test" {
172+ ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-x86_64.id
173+ instance_type = data.aws_ec2_instance_type_offering.available.instance_type
174+
175+ tags = {
176+ Name = %[1]q
177+ }
178+ }
179+
180+ action "aws_ec2_stop_instance" "test" {
181+ config {
182+ instance_id = aws_instance.test.id
183+ force = true
184+ }
185+ }
186+
187+ resource "terraform_data" "trigger" {
188+ input = "trigger"
189+ lifecycle {
190+ action_trigger {
191+ events = [before_create, before_update]
192+ actions = [action.aws_ec2_stop_instance.test]
193+ }
194+ }
195+ }
196+ ` , rName ))
197+ }
198+
140199// Step 1: Get the AWS provider as a ProviderServerWithActions
141200func providerWithActions (ctx context.Context , t * testing.T ) tfprotov5.ProviderServerWithActions { //nolint:staticcheck // SA1019: Working in alpha situation
142201 t .Helper ()
0 commit comments