Last active
June 30, 2020 17:34
-
-
Save SlootSantos/3349ad3897d73832c18427ac3529bfe8 to your computer and use it in GitHub Desktop.
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 ( | |
| "log" | |
| "github.com/aws/aws-sdk-go/aws" | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/iam" | |
| "github.com/aws/aws-sdk-go/service/s3" | |
| ) | |
| const ( | |
| bucketNameOrigin = "medium-lambda-go-sdk-origin-white" | |
| bucketNameSourceCode = "medium-lambda-go-sdk-source-code-white" | |
| bucketACLPublic = "public-read" | |
| iamRoleName = "stackers-cdn-routing-lambda-policy-white" | |
| ) | |
| func main() { | |
| log.Println("Hello Lambda") | |
| awsSess := initAWSSession() | |
| createS3Buckets(awsSess) | |
| uploadZip(awsSess) | |
| roleARN:= createIAMRole(awsSess) | |
| } | |
| func initAWSSession() *session.Session { | |
| s, err := session.NewSession(&aws.Config{ | |
| Region: aws.String("us-east-1"), | |
| }) | |
| if err != nil { | |
| panic("Did you pass the credentials?") | |
| } | |
| return s | |
| } | |
| func createS3Buckets(s *session.Session) { | |
| s3Handler := s3.New(s) | |
| createBucketInput := &s3.CreateBucketInput{ | |
| Bucket: aws.String(bucketNameOrigin), | |
| ACL: aws.String(bucketACLPublic), | |
| } | |
| _, err := s3Handler.CreateBucket(createBucketInput) | |
| if err != nil { | |
| panic("Could not create origin bucket" + err.Error()) | |
| } | |
| createSourceCodeBucketInput := &s3.CreateBucketInput{ | |
| Bucket: aws.String(bucketNameSourceCode), | |
| ACL: aws.String(bucketACLPublic), | |
| } | |
| _, err = s3Handler.CreateBucket(createSourceCodeBucketInput) | |
| if err != nil { | |
| panic("Could not create source code bucket" + err.Error()) | |
| } | |
| } | |
| func uploadZip(s *session.Session) { | |
| file, err := os.Open("source.zip") | |
| if err != nil { | |
| panic("Could not load source zip" + err.Error()) | |
| } | |
| defer file.Close() | |
| uploader := s3manager.NewUploader(s) | |
| _, err = uploader.Upload(&s3manager.UploadInput{ | |
| Bucket: aws.String(bucketNameSourceCode), | |
| Key: aws.String(filepath.Base("source.zip")), | |
| Body: file, | |
| }) | |
| if err != nil { | |
| panic("Could not upload source zip" + err.Error()) | |
| } | |
| } | |
| func createIAMRole(s *session.Session) string{ | |
| iamHandler := iam.New(s) | |
| role, err := iamHandler.CreateRole(&iam.CreateRoleInput{ | |
| Path: aws.String("/service-role/"), | |
| AssumeRolePolicyDocument: aws.String("{\"Version\": \"2012-10-17\",\"Statement\": [{\"Effect\": \"Allow\",\"Principal\": {\"Service\": [\"lambda.amazonaws.com\",\"edgelambda.amazonaws.com\"]},\"Action\": \"sts:AssumeRole\"}]}"), | |
| RoleName: aws.String(iamRoleName), | |
| }) | |
| if err != nil { | |
| panic("Could not create IAM policy"+ err.Error()) | |
| } | |
| return *role.Role.Arn | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment