Пример #1
0
// should probably handle this in memory,
// but this fn will upload your file to s3
func (d *deployer) uploadS3(fName string, bucket, key *string) error {
	cfg := d.cfg
	debug := debug.Debug("uploadS3")
	awscfg := aws.NewConfig().WithRegion(*cfg.Location.S3Region)
	s := s3.New(awscfg)

	f, err := os.Open(fName)
	if err != nil {
		return err
	}
	debug("opened file, beginning read")

	st, err := f.Stat()
	if err != nil {
		return err
	}

	if st.Size() >= (1024 * 1024 * 5) {
		// multipart if bigger than 5MiB
		return uploadS3MPU(s, f, bucket, key)
	} else {
		_, err = s.PutObject(&s3.PutObjectInput{
			Bucket: bucket,
			Key:    key,
			Body:   f,
		})
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #2
0
func (d *deployer) deploy(c *cobra.Command, args []string) error {
	debug := debug.Debug("cmd.deploy")
	// must package first, so:
	var err error

	if d.cfg == nil {
		return fmt.Errorf(
			`No configuration file found, and you must have one to deploy!
Try running lambda-phage init before deploying.
`)
	}

	fmt.Printf("Beginning deploy of lambda function %s\n", *d.cfg.Name)

	skipPkg, _ := c.Flags().GetBool("skip-archive")
	if !skipPkg {
		pkgEr := packager{
			cfg,
		}
		err = pkgEr.pkg(c, args)

		if err != nil {
			return err
		}
	}

	// should have written data to this file
	binName := getArchiveName(c, d.cfg)

	var iamRole *string
	iamRole, err = cfg.getRoleArn()
	if err != nil {
		return err
	}

	code := &lambda.FunctionCode{}
	// try getting s3 information
	bucket, key := cfg.getS3Info(binName)

	if bucket == nil || key == nil {
		fmt.Printf("Will upload archive %s to Lambda\n", binName)
		// if we couldn't get bucket or
		// key info, let's upload the data
		// ...soon
		b, err := ioutil.ReadFile(binName)
		if err != nil {
			return err
		}

		code.ZipFile = b
	} else {
		fmt.Printf("Will upload archive %s to s3\n", binName)
		code.S3Bucket = bucket
		code.S3Key = key
		err = d.uploadS3(binName, bucket, key)
		if err != nil {
			return err
		}
	}

	debug("preparing for lambda API")
	for _, region := range cfg.Regions {
		fmt.Printf("Deploying lambda function for %s\n", *region)

		l := lambda.New(
			aws.NewConfig().
				WithRegion(*region),
		)
		//just try creating the function now
		_, err := l.CreateFunction(
			&lambda.CreateFunctionInput{
				Code:         code,
				FunctionName: cfg.Name,
				Description:  cfg.Description,
				Handler:      cfg.EntryPoint,
				MemorySize:   cfg.MemorySize,
				Runtime:      cfg.Runtime,
				Timeout:      cfg.Timeout,
				Role:         iamRole,
				Publish:      aws.Bool(true),
			},
		)

		if err != nil {
			if awe, ok := err.(awserr.Error); ok {
				if awe.Code() == "ResourceConflictException" {
					debug("function already exists, calling update")
					err = d.updateLambda(l, code, iamRole)
				} else {
					return err
				}
			} else {
				return err
			}
		} else {
			// if the create function succeeded,
			// we need to figure out the mapping
			// info, etc
			debug("function creation succeeded! ...we think")
		}

		if err != nil {
			return err
		}

		fmt.Printf("Function %s deployed to region %s!\n", *cfg.Name, *region)
	}

	return nil
}
Пример #3
0
	"github.com/hopkinsth/lambda-phage/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
)

// DefaultChainCredentials is a Credentials which will find the first available
// credentials Value from the list of Providers.
//
// This should be used in the default case. Once the type of credentials are
// known switching to the specific Credentials will be more efficient.
var DefaultChainCredentials = credentials.NewChainCredentials(
	[]credentials.Provider{
		&credentials.EnvProvider{},
		&credentials.SharedCredentialsProvider{Filename: "", Profile: ""},
		&ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute},
	})

// DefaultConfig is the default all service configuration will be based off of.
// By default, all clients use this structure for initialization options unless
// a custom configuration object is passed in.
//
// You may modify this global structure to change all default configuration
// in the SDK. Note that configuration options are copied by value, so any
// modifications must happen before constructing a client.
var DefaultConfig = aws.NewConfig().
	WithCredentials(DefaultChainCredentials).
	WithRegion(os.Getenv("AWS_REGION")).
	WithHTTPClient(http.DefaultClient).
	WithMaxRetries(aws.DefaultRetries).
	WithLogger(aws.NewDefaultLogger()).
	WithLogLevel(aws.LogOff).
	WithSleepDelay(time.Sleep)