예제 #1
0
파일: main.go 프로젝트: fstehle/asg-ebs
func NewAwsAsgEbs(maxRetries int) *AwsAsgEbs {
	awsAsgEbs := &AwsAsgEbs{}

	metadata := ec2metadata.New(session.New())

	region, err := metadata.Region()
	if err != nil {
		log.WithFields(log.Fields{"error": err}).Fatal("Failed to get region from instance metadata")
	}
	log.WithFields(log.Fields{"region": region}).Info("Setting region")
	awsAsgEbs.Region = region

	availabilityZone, err := metadata.GetMetadata("placement/availability-zone")
	if err != nil {
		log.WithFields(log.Fields{"error": err}).Fatal("Failed to get availability zone from instance metadata")
	}
	log.WithFields(log.Fields{"az": availabilityZone}).Info("Setting availability zone")
	awsAsgEbs.AvailabilityZone = availabilityZone

	instanceId, err := metadata.GetMetadata("instance-id")
	if err != nil {
		log.WithFields(log.Fields{"error": err}).Fatal("Failed to get instance id from instance metadata")
	}
	log.WithFields(log.Fields{"instance_id": instanceId}).Info("Setting instance id")
	awsAsgEbs.InstanceId = instanceId

	awsAsgEbs.AwsConfig = aws.NewConfig().
		WithRegion(region).
		WithCredentials(ec2rolecreds.NewCredentials(session.New())).
		WithMaxRetries(maxRetries)

	return awsAsgEbs
}
예제 #2
0
파일: main.go 프로젝트: iPowow/lbManager
func getFromS3() error {

	// creds := &ec2rolecreds.EC2RoleProvider{
	// 	client: &http.Client{Timeout: 10 * time.Second},
	// 	window: 0,
	// }

	s3svc := s3.New(&aws.Config{
		Region:      aws.String(s3Region),
		Credentials: ec2rolecreds.NewCredentials(nil, 5*time.Minute),
	})

	params := &s3.GetObjectInput{
		Bucket: aws.String(p.bucket),
		Key:    aws.String(p.path),
	}
	resp, err := s3svc.GetObject(params)
	if err != nil {
		return err
	}

	outputPath := filepath.Join(p.outputDir, getFilename(p.path))
	artifact, err := os.Create(outputPath)
	if err != nil {
		return err
	}
	defer artifact.Close()

	if _, err := io.Copy(artifact, resp.Body); err != nil {
		return err
	}

	return nil
}