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

Enhancement: Step Functions for Express Workflows #11570

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 107 additions & 9 deletions aws/resource_aws_sfn_state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ func resourceAwsSfnStateMachine() *schema.Resource {
ValidateFunc: validation.StringLenBetween(0, 1024*1024), // 1048576
},

"logging_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_destination": {
Type: schema.TypeString,
Optional: true,
},
"include_execution_data": {
Type: schema.TypeBool,
Optional: true,
// Default: false,
},
"level": {
Type: schema.TypeString,
Optional: true,
// Default: sfn.LogLevelOff,
ValidateFunc: validation.StringInSlice([]string{
sfn.LogLevelAll,
sfn.LogLevelError,
sfn.LogLevelFatal,
sfn.LogLevelOff,
}, false),
},
},
},
},

"name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -53,20 +84,32 @@ func resourceAwsSfnStateMachine() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),

"type": {
Type: schema.TypeString,
Optional: true,
Default: sfn.StateMachineTypeStandard,
ValidateFunc: validation.StringInSlice([]string{
sfn.StateMachineTypeStandard,
sfn.StateMachineTypeExpress,
}, false),
},
},
}
}

func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Print("[DEBUG] Creating Step Function State Machine")

params := &sfn.CreateStateMachineInput{
Definition: aws.String(d.Get("definition").(string)),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
Definition: aws.String(d.Get("definition").(string)),
LoggingConfiguration: expandAwsSfnLoggingConfiguration(d.Get("logging_configuration").([]interface{})),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
Type: aws.String(d.Get("type").(string)),
}

var activity *sfn.CreateStateMachineOutput
Expand Down Expand Up @@ -104,7 +147,6 @@ func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{})
func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Printf("[DEBUG] Reading Step Function State Machine: %s", d.Id())

sm, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
StateMachineArn: aws.String(d.Id()),
})
Expand All @@ -122,8 +164,18 @@ func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) er
d.Set("definition", sm.Definition)
d.Set("name", sm.Name)
d.Set("role_arn", sm.RoleArn)
d.Set("type", sm.Type)
d.Set("status", sm.Status)

loggingConfiguration := flattenAwsSfnLoggingConfiguration(sm.LoggingConfiguration)

if loggingConfiguration != nil {
err := d.Set("logging_configuration", loggingConfiguration)
if err != nil {
log.Printf("[DEBUG] Error setting logging_configuration %s \n", err)
}
}

if err := d.Set("creation_date", sm.CreationDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting creation_date: %s", err)
}
Expand All @@ -150,10 +202,14 @@ func resourceAwsSfnStateMachineUpdate(d *schema.ResourceData, meta interface{})
RoleArn: aws.String(d.Get("role_arn").(string)),
}

_, err := conn.UpdateStateMachine(params)

log.Printf("[DEBUG] Updating Step Function State Machine: %#v", params)

if d.HasChange("logging_configuration") && d.Get("type").(string) == sfn.StateMachineTypeExpress {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if d.HasChange("logging_configuration") && d.Get("type").(string) == sfn.StateMachineTypeExpress {
if d.HasChange("logging_configuration") {

params.LoggingConfiguration = expandAwsSfnLoggingConfiguration(d.Get("logging_configuration").([]interface{}))
}

_, err := conn.UpdateStateMachine(params)

if err != nil {
if isAWSErr(err, "StateMachineDoesNotExist", "State Machine Does Not Exist") {
return fmt.Errorf("Error updating Step Function State Machine: %s", err)
Expand All @@ -168,7 +224,7 @@ func resourceAwsSfnStateMachineUpdate(d *schema.ResourceData, meta interface{})
}
}

return resourceAwsSfnStateMachineRead(d, meta)
return nil
}

func resourceAwsSfnStateMachineDelete(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -195,3 +251,45 @@ func resourceAwsSfnStateMachineDelete(d *schema.ResourceData, meta interface{})
}
return nil
}

func expandAwsSfnLoggingConfiguration(l []interface{}) *sfn.LoggingConfiguration {

if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

loggingConfiguration := &sfn.LoggingConfiguration{
Destinations: []*sfn.LogDestination{
{
CloudWatchLogsLogGroup: &sfn.CloudWatchLogsLogGroup{
LogGroupArn: aws.String(m["log_destination"].(string)),
},
},
},
IncludeExecutionData: aws.Bool(m["include_execution_data"].(bool)),
Level: aws.String(m["level"].(string)),
}

return loggingConfiguration
}

func flattenAwsSfnLoggingConfiguration(loggingConfiguration *sfn.LoggingConfiguration) []interface{} {

if loggingConfiguration == nil {
return []interface{}{}
}

m := map[string]interface{}{
"log_destination": "",
"include_execution_data": aws.BoolValue(loggingConfiguration.IncludeExecutionData),
"level": aws.StringValue(loggingConfiguration.Level),
}

if len(loggingConfiguration.Destinations) > 0 {
m["log_destination"] = aws.StringValue(loggingConfiguration.Destinations[0].CloudWatchLogsLogGroup.LogGroupArn)
}

return []interface{}{m}
}
Loading