일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- aws cli
- wireshark
- Sysinternals
- namespace
- Kubernetes
- Windows10
- Android
- aws
- ffmpeg
- nginx-media-server
- Shell script
- Pod
- golang
- configmap
- ebpf
- VSCode
- macos
- Flutter
- dart
- kubectl
- docker
- Java
- spring cloud config
- deployment
- 행정구역분류
- HLS
- android studio
- service
- Python
- RTMP
- Today
- Total
woonizzooni
AWS SDK for Golang - Session 재사용 본문
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 ConfigBy 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
참고
'AWS' 카테고리의 다른 글
AWS CLI 버전1 구성 (windows 10) (0) | 2021.05.24 |
---|---|
Amazon SQS : the specified queue does not exist or you do not have access to it (0) | 2020.12.30 |
aws cli 명령 출력 제어 - Autoscaling 조회 using dictionary notation, 필터링... (0) | 2020.11.09 |
EKS 클러스터 인증 관리 (0) | 2020.03.30 |
AWS CLI 구성 (MacOS) (0) | 2020.03.29 |