Last active
September 21, 2017 20:38
-
-
Save hirokazumiyaji/657c03b51606461b0dab to your computer and use it in GitHub Desktop.
consul deploy using AWS Lambda Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"path/filepath" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"github.com/aws/aws-sdk-go/service/s3/s3manager" | |
) | |
var ( | |
bucketName = flag.String("bucketname") | |
key = flag.String("key") | |
outputPath = flag.String("output") | |
) | |
func main() { | |
flag.Parse() | |
fmt.Printf("Download From S3 Start. Bucket: %s, Key: %s\n", *bucketName, *key) | |
svc := s3.New(&aws.Config{Region: aws.String("us-west-2")}) | |
input := &s3.CopyObjectInput{Bucket: bucketName, Key: key} | |
downloader := s3manager.NewDownloader(&s3manager.DownloadOptions{}) | |
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil { | |
log.Fatal("Error Make Directory: ", err.Error()) | |
return | |
} | |
fd, err := os.Create(outputPath) | |
if err != nil { | |
log.Fatal("Error Create File: ", err.Error()) | |
return | |
} | |
defer fd.Close() | |
n, err := downloader.Download(fd, input) | |
if err != nil { | |
log.Fatal("Error Download From S3: ", err.Error()) | |
return | |
} | |
fmt.Println("Download from S3 Finish.") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("Pre Deploy Start."); | |
export.handler = function(event, context) { | |
var bucketName = event.Records[0].s3.bucket.name; | |
var key = event.Records[0].s3.object.key; | |
var consul = require('consul')(); | |
consul.event.fire('predeploy', JSON.stringify(BucketName: bucketName, Key: key}), function(err, result){ | |
if (err) { | |
console.log("Pre Deploy Error. " + err); | |
throw err; | |
} | |
console.log("Pre Deploy End."); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment