woonizzooni

AWS SDK for Golang - Session 재사용 본문

AWS

AWS SDK for Golang - Session 재사용

woonizzooni 2020. 11. 6. 23:47

 

AWS SDK for Go

가능하면 session을 캐시하고 쓰라고 하네?

동시성에도 안전하니(수정하지/되지 않는한) 재사용 하자!

 

docs.aws.amazon.com/sdk-for-go/api/aws/session/

Overview ▾

Package session provides configuration for the SDK's service clients. Sessions can be shared across service clients that share the same base configuration.

Sessions are safe to use concurrently as long as the Session is not being modified. Sessions should be cached when possible, because creating a new Session will load all configuration values from the environment, and config files each time the Session is created. Sharing the Session value across all of your service clients will ensure the configuration is loaded the fewest number of times possible.

Sessions options from Shared Config

By default NewSession will only load credentials from the shared credentials file (~/.aws/credentials). If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value the Session will be created from the configuration values from the shared config (~/.aws/config) and shared credentials (~/.aws/credentials) files. Using the NewSessionWithOptions with SharedConfigState set to SharedConfigEnable will create the session as if the AWS_SDK_LOAD_CONFIG environment variable was set.

 

ex)

  - EC2/EKS/S3/EMS/Translate/Transcribe등 주요 서비스 이용/연동할 때 마다가 아닌,

    최초 세션 생성해두고 이를 재사용하도록 하자.

---- main.go
func main() {
    // initialize
    ac := aws.NewClient(ctx)
    
    // processing ..
    ac.EC2Svc.DescribeInstancesWithTag("nameFilterXXX")
    ac.ASGSvc.DescribeAutoScalingGroups("service-worker-xxxx")
    ac.ECRSvc.GetAuthorizationToken("ap-northeast-2")
    ac.EKSSvc.ListClusters()
    
    // finish
}

---- client.go
type Client struct {
    ...
    session *session.Session
    common  service
    ASGSvc  *autoScalingService
    EC2Svc  *ec2Service
    ECRSvc  *ecrService
    EKSSvc  *eksService
	...
}

type service struct {
    client *Client
}

// NewClient ...
func NewClient(region string) (*Client, error) {
    c := &Client{}
    c.common.client = c

    sess, err := session.NewSession(&aws.Config{
        Region: aws.String(region)},
    )
    ...
    c.session = session.Must(sess, err)

    c.ASGSvc = (*autoScalingService)(&c.common)
    c.EC2Svc = (*ec2Service)(&c.common)
    c.ECRSvc = (*ecrService)(&c.common)
    c.EKSSvc = (*eksService)(&c.common)

    ...
    return c, nil
}

// GetEC2Client ...
func (c *Client) GetEC2Client() *ec2.EC2 {
    if c.EC2 == nil {
        c.EC2 = ec2.New(c.session)
    }
    return c.EC2
}

// GetAutoScalingClient ...
func (c *Client) GetAutoScalingClient() *autoscaling.AutoScaling {
    if c.ASG == nil {
        c.ASG = autoscaling.New(c.session)
    }
    return c.ASG
}

// GetECRClient ...
func (c *Client) GetECRClient() *ecr.ECR {
    if c.ECR == nil {
        c.ECR = ecr.New(c.session)
    }
    return c.ECR
}

// GetEKSClient ...
func (c *Client) GetEKSClient() *eks.EKS {
    if c.EKS == nil {
        c.EKS = eks.New(c.session)
    }
    return c.EKS
}
...


---- {service}.go
skip

참고

github.com/aws/aws-sdk-go

 

Comments