func ExampleS3_HeadObject() { svc := s3.New(session.New()) params := &s3.HeadObjectInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required IfMatch: aws.String("IfMatch"), IfModifiedSince: aws.Time(time.Now()), IfNoneMatch: aws.String("IfNoneMatch"), IfUnmodifiedSince: aws.Time(time.Now()), Range: aws.String("Range"), RequestPayer: aws.String("RequestPayer"), SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"), SSECustomerKey: aws.String("SSECustomerKey"), SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"), VersionId: aws.String("ObjectVersionId"), } resp, err := svc.HeadObject(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleCloudWatchLogs_PutLogEvents() { svc := cloudwatchlogs.New(session.New()) params := &cloudwatchlogs.PutLogEventsInput{ LogEvents: []*cloudwatchlogs.InputLogEvent{ // Required { // Required Message: aws.String("EventMessage"), // Required Timestamp: aws.Int64(1), // Required }, // More values... }, LogGroupName: aws.String("LogGroupName"), // Required LogStreamName: aws.String("LogStreamName"), // Required SequenceToken: aws.String("SequenceToken"), } resp, err := svc.PutLogEvents(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func Notify(name, status string, data map[string]string) error { if PauseNotifications { return nil } log := logger.New("ns=kernel") data["rack"] = os.Getenv("RACK") event := &client.NotifyEvent{ Action: name, Status: status, Data: data, Timestamp: time.Now().UTC(), } message, err := json.Marshal(event) if err != nil { return err } params := &sns.PublishInput{ Message: aws.String(string(message)), // Required Subject: aws.String(name), TargetArn: aws.String(NotificationTopic), } resp, err := SNS().Publish(params) if err != nil { return err } log.At("Notfiy").Log("message-id=%q", *resp.MessageId) return nil }
func (b *Build) log(line string) { b.Logs += fmt.Sprintf("%s\n", line) if b.kinesis == "" { app, err := GetApp(b.App) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s", err) return } b.kinesis = app.Outputs["Kinesis"] } _, err := Kinesis().PutRecords(&kinesis.PutRecordsInput{ StreamName: aws.String(b.kinesis), Records: []*kinesis.PutRecordsRequestEntry{ &kinesis.PutRecordsRequestEntry{ Data: []byte(fmt.Sprintf("build: %s", line)), PartitionKey: aws.String(string(time.Now().UnixNano())), }, }, }) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) } }
func ListReleases(app string) (Releases, error) { req := &dynamodb.QueryInput{ KeyConditions: map[string]*dynamodb.Condition{ "app": &dynamodb.Condition{ AttributeValueList: []*dynamodb.AttributeValue{ &dynamodb.AttributeValue{S: aws.String(app)}, }, ComparisonOperator: aws.String("EQ"), }, }, IndexName: aws.String("app.created"), Limit: aws.Int64(20), ScanIndexForward: aws.Bool(false), TableName: aws.String(releasesTable(app)), } res, err := DynamoDB().Query(req) if err != nil { return nil, err } releases := make(Releases, len(res.Items)) for i, item := range res.Items { releases[i] = *releaseFromItem(item) } return releases, nil }
func SNSSubscriptionDelete(req Request) (string, map[string]string, error) { if req.PhysicalResourceId == "failed" { return req.PhysicalResourceId, nil, nil } topicArn := req.ResourceProperties["TopicArn"].(string) params := &sns.ListSubscriptionsByTopicInput{ TopicArn: aws.String(topicArn), } resp, err := SNS(req).ListSubscriptionsByTopic(params) if err != nil { fmt.Printf("error: %s\n", err) return req.PhysicalResourceId, nil, nil } for _, s := range resp.Subscriptions { if *s.Endpoint == req.PhysicalResourceId { _, err := SNS(req).Unsubscribe(&sns.UnsubscribeInput{ SubscriptionArn: aws.String(*s.SubscriptionArn), }) if err != nil { fmt.Printf("error: %s\n", err) return req.PhysicalResourceId, nil, nil } return *s.Endpoint, nil, nil } } return req.PhysicalResourceId, nil, nil }
func ExampleKinesis_PutRecords() { svc := kinesis.New(session.New()) params := &kinesis.PutRecordsInput{ Records: []*kinesis.PutRecordsRequestEntry{ // Required { // Required Data: []byte("PAYLOAD"), // Required PartitionKey: aws.String("PartitionKey"), // Required ExplicitHashKey: aws.String("HashKey"), }, // More values... }, StreamName: aws.String("StreamName"), // Required } resp, err := svc.PutRecords(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleCloudWatch_DescribeAlarms() { svc := cloudwatch.New(session.New()) params := &cloudwatch.DescribeAlarmsInput{ ActionPrefix: aws.String("ActionPrefix"), AlarmNamePrefix: aws.String("AlarmNamePrefix"), AlarmNames: []*string{ aws.String("AlarmName"), // Required // More values... }, MaxRecords: aws.Int64(1), NextToken: aws.String("NextToken"), StateValue: aws.String("StateValue"), } resp, err := svc.DescribeAlarms(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func (instances Instances) describeECS() error { res, err := models.ECS().ListContainerInstances( &ecs.ListContainerInstancesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), }, ) if err != nil { return err } dres, err := models.ECS().DescribeContainerInstances( &ecs.DescribeContainerInstancesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), ContainerInstances: res.ContainerInstanceArns, }, ) if err != nil { return err } for _, i := range dres.ContainerInstances { instance := instances[*i.Ec2InstanceId] instance.Id = *i.Ec2InstanceId instance.ECS = *i.AgentConnected instances[*i.Ec2InstanceId] = instance } return nil }
func GetRelease(app, id string) (*Release, error) { if id == "" { return nil, fmt.Errorf("no release id") } req := &dynamodb.GetItemInput{ ConsistentRead: aws.Bool(true), Key: map[string]*dynamodb.AttributeValue{ "id": &dynamodb.AttributeValue{S: aws.String(id)}, }, TableName: aws.String(releasesTable(app)), } res, err := DynamoDB().GetItem(req) if err != nil { return nil, err } if res.Item == nil { return nil, fmt.Errorf("no such release: %s", id) } release := releaseFromItem(res.Item) return release, nil }
func ExampleCloudWatch_ListMetrics() { svc := cloudwatch.New(session.New()) params := &cloudwatch.ListMetricsInput{ Dimensions: []*cloudwatch.DimensionFilter{ { // Required Name: aws.String("DimensionName"), // Required Value: aws.String("DimensionValue"), }, // More values... }, MetricName: aws.String("MetricName"), Namespace: aws.String("Namespace"), NextToken: aws.String("NextToken"), } resp, err := svc.ListMetrics(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ClusterServices() (ECSServices, error) { var log = logger.New("ns=ClusterServices") services := ECSServices{} lsres, err := ECS().ListServices(&ecs.ListServicesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), }) if err != nil { log.Log("at=ListServices err=%q", err) return services, err } dsres, err := ECS().DescribeServices(&ecs.DescribeServicesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), Services: lsres.ServiceArns, }) if err != nil { log.Log("at=ListServices err=%q", err) return services, err } for i := 0; i < len(dsres.Services); i++ { services = append(services, dsres.Services[i]) } return services, nil }
func (p *AWSProvider) clusterServices() (ECSServices, error) { services := ECSServices{} lsres, err := p.ecs().ListServices(&ecs.ListServicesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), }) if err != nil { return services, err } dsres, err := p.ecs().DescribeServices(&ecs.DescribeServicesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), Services: lsres.ServiceArns, }) if err != nil { return services, err } for i := 0; i < len(dsres.Services); i++ { services = append(services, dsres.Services[i]) } return services, nil }
func GetAppServices(app string) ([]*ecs.Service, error) { services := []*ecs.Service{} resources, err := ListResources(app) if err != nil { return services, err } arns := []*string{} for _, r := range resources { if r.Type == "Custom::ECSService" { arns = append(arns, aws.String(r.Id)) } } dres, err := ECS().DescribeServices(&ecs.DescribeServicesInput{ Cluster: aws.String(os.Getenv("CLUSTER")), Services: arns, }) if err != nil { return services, err } return dres.Services, nil }
func TestInputService5ProtocolTestRecursiveShapesCase3(t *testing.T) { sess := session.New() svc := NewInputService5ProtocolTest(sess, &aws.Config{Endpoint: aws.String("https://test")}) input := &InputService5TestShapeInputShape{ RecursiveStruct: &InputService5TestShapeRecursiveStructType{ RecursiveStruct: &InputService5TestShapeRecursiveStructType{ RecursiveStruct: &InputService5TestShapeRecursiveStructType{ RecursiveStruct: &InputService5TestShapeRecursiveStructType{ NoRecurse: aws.String("foo"), }, }, }, }, } req, _ := svc.InputService5TestCaseOperation3Request(input) r := req.HTTPRequest // build request jsonrpc.Build(req) assert.NoError(t, req.Error) // assert body assert.NotNil(t, r.Body) body, _ := ioutil.ReadAll(r.Body) awstesting.AssertJSON(t, `{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"NoRecurse":"foo"}}}}}`, util.Trim(string(body))) // assert URL awstesting.AssertURL(t, "https://test/", r.URL.String()) // assert headers assert.Equal(t, "application/x-amz-json-1.1", r.Header.Get("Content-Type")) assert.Equal(t, "com.amazonaws.foo.OperationName", r.Header.Get("X-Amz-Target")) }
func TestDownloadError(t *testing.T) { s, names, _ := dlLoggingSvc([]byte{1, 2, 3}) num := 0 s.Handlers.Send.PushBack(func(r *request.Request) { num++ if num > 1 { r.HTTPResponse.StatusCode = 400 r.HTTPResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte{})) } }) d := s3manager.NewDownloaderWithClient(s, func(d *s3manager.Downloader) { d.Concurrency = 1 d.PartSize = 1 }) w := &aws.WriteAtBuffer{} n, err := d.Download(w, &s3.GetObjectInput{ Bucket: aws.String("bucket"), Key: aws.String("key"), }) assert.NotNil(t, err) assert.Equal(t, int64(1), n) assert.Equal(t, []string{"GetObject", "GetObject"}, *names) assert.Equal(t, []byte{1}, w.Bytes()) }
// Retrieve generates a new set of temporary credentials using STS. func (p *AssumeRoleProvider) Retrieve() (credentials.Value, error) { // Apply defaults where parameters are not set. if p.Client == nil { p.Client = sts.New(nil) } if p.RoleSessionName == "" { // Try to work out a role name that will hopefully end up unique. p.RoleSessionName = fmt.Sprintf("%d", time.Now().UTC().UnixNano()) } if p.Duration == 0 { // Expire as often as AWS permits. p.Duration = 15 * time.Minute } roleOutput, err := p.Client.AssumeRole(&sts.AssumeRoleInput{ DurationSeconds: aws.Int64(int64(p.Duration / time.Second)), RoleArn: aws.String(p.RoleARN), RoleSessionName: aws.String(p.RoleSessionName), ExternalId: p.ExternalID, }) if err != nil { return credentials.Value{}, err } // We will proactively generate new credentials before they expire. p.SetExpiration(*roleOutput.Credentials.Expiration, p.ExpiryWindow) return credentials.Value{ AccessKeyID: *roleOutput.Credentials.AccessKeyId, SecretAccessKey: *roleOutput.Credentials.SecretAccessKey, SessionToken: *roleOutput.Credentials.SessionToken, }, nil }
func subscribeCloudWatchLogsStream(group, stream string, startTime time.Time, output chan []byte, quit chan bool) { log := logger.New("at=subscribe-cloudwatch").Start() fmt.Printf("subscribeCloudWatchLogsStream group=%s stream=%s startTime=%s\n", group, stream, startTime) startTimeMs := startTime.Unix() * 1000 // ms since epoch req := cloudwatchlogs.GetLogEventsInput{ LogGroupName: aws.String(group), LogStreamName: aws.String(stream), } for { select { case <-quit: log.Log("qutting") return default: req.StartTime = &startTimeMs res, err := CloudWatchLogs().GetLogEvents(&req) if err != nil { fmt.Printf("err3 %+v\n", err) return } for _, event := range res.Events { output <- []byte(fmt.Sprintf("%s\n", string(*event.Message))) startTimeMs = *event.Timestamp + 1 } time.Sleep(1000 * time.Millisecond) } } }
func (s *Service) UpdatePapertrail(arns map[string]string) error { input := struct { ARNs map[string]string }{ arns, } formation, err := buildTemplate(fmt.Sprintf("service/%s", s.Type), "service", input) if err != nil { return err } // Update stack with all linked ARNs and EventSourceMappings _, err = CloudFormation().UpdateStack(&cloudformation.UpdateStackInput{ StackName: aws.String(s.Name), Capabilities: []*string{aws.String("CAPABILITY_IAM")}, Parameters: []*cloudformation.Parameter{ &cloudformation.Parameter{ ParameterKey: aws.String("Url"), ParameterValue: aws.String(s.Parameters["Url"]), }, }, TemplateBody: aws.String(formation), }) return err }
func ExampleKMS_GenerateDataKeyWithoutPlaintext() { svc := kms.New(session.New()) params := &kms.GenerateDataKeyWithoutPlaintextInput{ KeyId: aws.String("KeyIdType"), // Required EncryptionContext: map[string]*string{ "Key": aws.String("EncryptionContextValue"), // Required // More values... }, GrantTokens: []*string{ aws.String("GrantTokenType"), // Required // More values... }, KeySpec: aws.String("DataKeySpec"), NumberOfBytes: aws.Int64(1), } resp, err := svc.GenerateDataKeyWithoutPlaintext(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ECSServiceUpdate(req Request) (string, map[string]string, error) { count, _ := strconv.Atoi(req.ResourceProperties["DesiredCount"].(string)) // arn:aws:ecs:us-east-1:922560784203:service/sinatra-SZXTRXEMYEY parts := strings.Split(req.PhysicalResourceId, "/") name := parts[1] replace, err := ECSServiceReplacementRequired(req) if err != nil { return req.PhysicalResourceId, nil, err } if replace { return ECSServiceCreate(req) } res, err := ECS(req).UpdateService(&ecs.UpdateServiceInput{ Cluster: aws.String(req.ResourceProperties["Cluster"].(string)), Service: aws.String(name), DesiredCount: aws.Int64(int64(count)), TaskDefinition: aws.String(req.ResourceProperties["TaskDefinition"].(string)), }) if err != nil { return req.PhysicalResourceId, nil, err } return *res.Service.ServiceArn, nil, nil }
func ExampleKMS_ReEncrypt() { svc := kms.New(session.New()) params := &kms.ReEncryptInput{ CiphertextBlob: []byte("PAYLOAD"), // Required DestinationKeyId: aws.String("KeyIdType"), // Required DestinationEncryptionContext: map[string]*string{ "Key": aws.String("EncryptionContextValue"), // Required // More values... }, GrantTokens: []*string{ aws.String("GrantTokenType"), // Required // More values... }, SourceEncryptionContext: map[string]*string{ "Key": aws.String("EncryptionContextValue"), // Required // More values... }, } resp, err := svc.ReEncrypt(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
// Shortcut for updating current parameters // If template changed, more care about new or removed parameters must be taken (see Release.Promote or System.Save) func (a *App) UpdateParams(changes map[string]string) error { req := &cloudformation.UpdateStackInput{ StackName: aws.String(a.StackName()), Capabilities: []*string{aws.String("CAPABILITY_IAM")}, UsePreviousTemplate: aws.Bool(true), } params := a.Parameters for key, val := range changes { params[key] = val } // sort parameters by key name to make test requests stable var keys []string for k, _ := range params { keys = append(keys, k) } sort.Strings(keys) for _, key := range keys { val := params[key] req.Parameters = append(req.Parameters, &cloudformation.Parameter{ ParameterKey: aws.String(key), ParameterValue: aws.String(val), }) } _, err := UpdateStack(req) return err }
func (s *Service) UpdateDatastore() (*cloudformation.UpdateStackInput, error) { formation, err := s.Formation() if err != nil { return nil, err } // Special case new parameters that don't have a default value // SubnetsPrivate is a List<AWS::EC2::Subnet::Id> and can not be empty // So reuse SUBNETS if SUBNETS_PRIVATE is not set subnetsPrivate := os.Getenv("SUBNETS_PRIVATE") subnetsPrivate := os.Getenv("SUBNETS_PRIVATE") if subnetsPrivate == "" { subnetsPrivate = os.Getenv("SUBNETS") } s.Parameters["SubnetsPrivate"] = subnetsPrivate req := &cloudformation.UpdateStackInput{ StackName: aws.String(s.StackName()), TemplateBody: aws.String(formation), } return req, nil }
func ExampleCloudWatchLogs_FilterLogEvents() { svc := cloudwatchlogs.New(session.New()) params := &cloudwatchlogs.FilterLogEventsInput{ LogGroupName: aws.String("LogGroupName"), // Required EndTime: aws.Int64(1), FilterPattern: aws.String("FilterPattern"), Interleaved: aws.Bool(true), Limit: aws.Int64(1), LogStreamNames: []*string{ aws.String("LogStreamName"), // Required // More values... }, NextToken: aws.String("NextToken"), StartTime: aws.Int64(1), } resp, err := svc.FilterLogEvents(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func (s *Service) CreateDatastore() (*cloudformation.CreateStackInput, error) { formation, err := s.Formation() if err != nil { return nil, err } if s.Type != "redis" { s.Parameters["Password"] = generateId("", 30) } // SubnetsPrivate is a List<AWS::EC2::Subnet::Id> and can not be empty // So reuse SUBNETS if SUBNETS_PRIVATE is not set subnetsPrivate := os.Getenv("SUBNETS_PRIVATE") if subnetsPrivate == "" { subnetsPrivate = os.Getenv("SUBNETS") } s.Parameters["Subnets"] = os.Getenv("SUBNETS") s.Parameters["SubnetsPrivate"] = subnetsPrivate s.Parameters["Vpc"] = os.Getenv("VPC") req := &cloudformation.CreateStackInput{ StackName: aws.String(s.StackName()), TemplateBody: aws.String(formation), } return req, nil }
func (a *App) UpdateParamsAndTemplate(changes map[string]string, template string) error { req := &cloudformation.UpdateStackInput{ StackName: aws.String(a.Name), Capabilities: []*string{aws.String("CAPABILITY_IAM")}, } if template != "" { req.TemplateURL = aws.String(template) } else { req.UsePreviousTemplate = aws.Bool(true) } params := a.Parameters for key, val := range changes { params[key] = val } for key, val := range params { req.Parameters = append(req.Parameters, &cloudformation.Parameter{ ParameterKey: aws.String(key), ParameterValue: aws.String(val), }) } _, err := UpdateStack(req) return err }
func ExampleECR_BatchGetImage() { svc := ecr.New(session.New()) params := &ecr.BatchGetImageInput{ ImageIds: []*ecr.ImageIdentifier{ // Required { // Required ImageDigest: aws.String("ImageDigest"), ImageTag: aws.String("ImageTag"), }, // More values... }, RepositoryName: aws.String("RepositoryName"), // Required RegistryId: aws.String("RegistryId"), } resp, err := svc.BatchGetImage(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func (s *Service) CreateWebhook() (*cloudformation.CreateStackInput, error) { var input interface{} formation, err := buildTemplate("service/webhook", "service", input) if err != nil { return nil, err } if s.Parameters["Url"] != "" { //ensure valid URL _, err = url.Parse(s.Parameters["Url"]) if err != nil { return nil, err } encEndpoint := url.QueryEscape(s.Parameters["Url"]) //NOTE: using http SNS notifier because https // doesn't work with rack's self-signed cert proxyEndpoint := "http://" + NotificationHost + "/sns?endpoint=" + encEndpoint s.Parameters["Url"] = proxyEndpoint } s.Parameters["NotificationTopic"] = NotificationTopic s.Parameters["CustomTopic"] = CustomTopic req := &cloudformation.CreateStackInput{ //TODO: do i need this? Capabilities: []*string{aws.String("CAPABILITY_IAM")}, StackName: aws.String(s.Name), TemplateBody: aws.String(formation), } return req, nil }
func ExampleS3_CompleteMultipartUpload() { svc := s3.New(session.New()) params := &s3.CompleteMultipartUploadInput{ Bucket: aws.String("BucketName"), // Required Key: aws.String("ObjectKey"), // Required UploadId: aws.String("MultipartUploadId"), // Required MultipartUpload: &s3.CompletedMultipartUpload{ Parts: []*s3.CompletedPart{ { // Required ETag: aws.String("ETag"), PartNumber: aws.Int64(1), }, // More values... }, }, RequestPayer: aws.String("RequestPayer"), } resp, err := svc.CompleteMultipartUpload(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }