forked from hashicorp/terraform-provider-aws
/
data_source_aws_lambda_invocation.go
95 lines (76 loc) · 2.04 KB
/
data_source_aws_lambda_invocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package aws
import (
"crypto/md5"
"encoding/json"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func dataSourceAwsLambdaInvocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLambdaInvocationRead,
Schema: map[string]*schema.Schema{
"function_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"qualifier": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "$LATEST",
},
"input": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.ValidateJsonString,
},
"result": {
Type: schema.TypeString,
Computed: true,
},
"result_map": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceAwsLambdaInvocationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
functionName := d.Get("function_name").(string)
qualifier := d.Get("qualifier").(string)
input := []byte(d.Get("input").(string))
res, err := conn.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(functionName),
InvocationType: aws.String(lambda.InvocationTypeRequestResponse),
Payload: input,
Qualifier: aws.String(qualifier),
})
if err != nil {
return err
}
if res.FunctionError != nil {
return fmt.Errorf("Lambda function (%s) returned error: (%s)", functionName, string(res.Payload))
}
if err = d.Set("result", string(res.Payload)); err != nil {
return err
}
var result map[string]interface{}
if err = json.Unmarshal(res.Payload, &result); err != nil {
return err
}
if err = d.Set("result_map", result); err != nil {
log.Printf("[WARN] Cannot use the result invocation as a string map: %s", err)
}
d.SetId(fmt.Sprintf("%s_%s_%x", functionName, qualifier, md5.Sum(input)))
return nil
}