Created
October 9, 2024 11:31
-
-
Save tysonpaul89/9bcadcc53e1db1ccafc0e0c424daba7e to your computer and use it in GitHub Desktop.
Go lang AWS SDK Wrapper Package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // AWS SDK wrapper package | |
| package aws | |
| import ( | |
| "context" | |
| "fmt" | |
| "strings" | |
| "github.com/aws/aws-sdk-go-v2/aws" | |
| "github.com/aws/aws-sdk-go-v2/config" | |
| "github.com/aws/aws-sdk-go-v2/service/lambda" | |
| "github.com/aws/aws-sdk-go-v2/service/ssm" | |
| "github.com/aws/aws-sdk-go-v2/service/sts" | |
| ) | |
| type LambdaEvent struct { | |
| Path string `json:"path"` | |
| Body string `json:"body"` | |
| HttpMethod string `json:"httpMethod"` | |
| } | |
| type LambdaResponse struct { | |
| StatusCode int `json:"statusCode"` | |
| IsBase64Encoded bool `json:"isBase64Encoded"` | |
| Headers map[string]string `json:"headers"` | |
| Body string `json:"body"` | |
| } | |
| // accountType is a global variable to store the AWS account type(prod or non-prod) | |
| var AccountType uint8 = 0 | |
| // cachedAWSParams is a global variable used to store the parameter store data. | |
| var cachedAWSParams = make(map[string]map[string]string) | |
| // Gets the given parameters from AWS ParameterStore | |
| func GetParameters(env string, awsParams []string) (map[string]string, error) { | |
| // In this method the give parameter is first looked up in the cachedAWSParams variable if it is not present, then | |
| // it will get it from AWS. The new values fetched from AWS is stored in the cachedAWSParams for future purpose. | |
| var availableParams = make(map[string]string) | |
| var requiredParams []string | |
| // Storing the already available AWS param store values to availableParams map | |
| // And storing the unavailable AWS param path name in the requiredParams to fetch it | |
| if existingAWSParams, ok := cachedAWSParams[env]; ok { | |
| for _, path := range awsParams { | |
| if value, ok := existingAWSParams[path]; ok { | |
| availableParams[path] = value | |
| } else { | |
| requiredParams = append(requiredParams, path) | |
| } | |
| } | |
| } else { | |
| requiredParams = awsParams | |
| } | |
| var awsParamPaths []string | |
| for _, path := range requiredParams { | |
| awsParamPaths = append(awsParamPaths, fmt.Sprintf("%s.%s", env, path)) | |
| } | |
| // Gets the parameters using AWS SDK | |
| if len(awsParamPaths) > 0 { | |
| res, err := GetParamsFromAwsParameterStore(awsParamPaths) | |
| if err != nil { | |
| return nil, err | |
| } | |
| for _, parameter := range res.Parameters { | |
| key := strings.Replace(*parameter.Name, fmt.Sprintf("%s.", env), "", 1) | |
| if _, ok := cachedAWSParams[env]; !ok { | |
| cachedAWSParams[env] = make(map[string]string) | |
| } | |
| cachedAWSParams[env][key] = *parameter.Value | |
| availableParams[key] = *parameter.Value | |
| } | |
| } | |
| return availableParams, nil | |
| } | |
| // Function returns the details about the current IAM user or role | |
| func GetAwsAccountDetails() (*sts.GetCallerIdentityOutput, error) { | |
| // Load AWS SDK configuration | |
| cfg, err := config.LoadDefaultConfig(context.TODO()) | |
| if err != nil { | |
| return nil, err | |
| } | |
| // Create Lambda client | |
| stsClient := sts.NewFromConfig(cfg) | |
| input := &sts.GetCallerIdentityInput{} | |
| // Gets account information of current user | |
| stsReq, err := stsClient.GetCallerIdentity(context.TODO(), input) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return stsReq, nil | |
| } | |
| // Calls the Parameter Store service using the AWS SDK and gets the parameter data | |
| func GetParamsFromAwsParameterStore(awsParamPaths []string) (*ssm.GetParametersOutput, error) { | |
| cfg, err := config.LoadDefaultConfig(context.TODO()) | |
| if err != nil { | |
| return nil, err | |
| } | |
| client := ssm.NewFromConfig(cfg) | |
| res, err := client.GetParameters(context.TODO(), &ssm.GetParametersInput{ | |
| Names: awsParamPaths, | |
| WithDecryption: aws.Bool(true), | |
| }) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return res, nil | |
| } | |
| // Invokes the given lambda with the given event data and returns the json response | |
| func InvokeLambdaFunction(lambdaName, payload string) (string, error) { | |
| cfg, err := config.LoadDefaultConfig(context.TODO()) | |
| if err != nil { | |
| return "", err | |
| } | |
| client := lambda.NewFromConfig(cfg) | |
| invokeOutput, err := client.Invoke(context.TODO(), &lambda.InvokeInput{ | |
| FunctionName: aws.String(lambdaName), | |
| Payload: []byte(payload), | |
| }) | |
| if err != nil { | |
| return "", err | |
| } | |
| return string(invokeOutput.Payload), nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment