LoginSignup
8
5

More than 5 years have passed since last update.

golang AWS-SDK (github.com/aws/aws-sdk-go)でEC2IAMRole権限でアクセスする

Last updated at Posted at 2016-03-18
s3.go
package main

import (
    "net/http"
    "time"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
    "github.com/aws/aws-sdk-go/aws/ec2metadata"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

// AwsConfig get config
func AwsConfig() *aws.Config {
    ec2m := ec2metadata.New(session.New(), &aws.Config{
        HTTPClient: &http.Client{Timeout: 10 * time.Second},
    })
    cr := credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
        Client: ec2m,
    })
    return &aws.Config{
        Region:      aws.String("ap-northeast-1"),
        Credentials: cr,
    }
}

func main() {
    s3c := s3.New(session.New(), AwsConfig())
    ...
}

credentialsファイルが有ればそっちを見て、なければIAMRoleを使うみたいなのを作りました
https://github.com/ushios/awsconfig

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5