Read real config for the AWS region
This change uses the machinery added to pulumi/lumi#117 to read the live AWS region config state, and prefer it to `AWS_REGION`. We still respect the region environment variable -- since this is the common way to do configuration like this with AWS tools -- but this at least avoids the issue of AWS_REGION being different and having your Lumi scripts compute differing regions and failing impressively.
This commit is contained in:
parent
97deabb9bd
commit
dae6736772
|
@ -18,6 +18,7 @@ package awsctx
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/apigateway"
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
||||
|
@ -27,6 +28,8 @@ import (
|
|||
"github.com/aws/aws-sdk-go/service/lambda"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/golang/glog"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pulumi/lumi/pkg/resource/provider"
|
||||
"github.com/pulumi/lumi/pkg/util/contract"
|
||||
|
||||
"github.com/pulumi/lumi/lib/aws/provider/arn"
|
||||
|
@ -50,13 +53,25 @@ type Context struct {
|
|||
s3 *s3.S3
|
||||
}
|
||||
|
||||
func New() (*Context, error) {
|
||||
const regionConfig = "aws:config:region"
|
||||
|
||||
func New(host *provider.HostClient) (*Context, error) {
|
||||
// Create an AWS session; note that this is safe to share among many operations.
|
||||
glog.V(5).Infof("Creating a new AWS session object w/ default credentials")
|
||||
// IDEA: consider verifying credentials, region, etc. here.
|
||||
// IDEA: currently we just inherit the standard AWS SDK credentials logic; eventually we will want more
|
||||
// flexibility, I assume, including possibly reading from configuration dynamically.
|
||||
sess, err := session.NewSession()
|
||||
var config []*aws.Config
|
||||
reg, err := host.ReadLocation(regionConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !reg.IsNull() {
|
||||
if !reg.IsString() {
|
||||
return nil, errors.Errorf("Expected a string for AWS region config '%v'; got %v", regionConfig, reg)
|
||||
}
|
||||
config = append(config, &aws.Config{Region: aws.String(reg.StringValue())})
|
||||
}
|
||||
sess, err := session.NewSession(config...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -16,40 +16,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/pulumi/lumi/pkg/resource/provider"
|
||||
"github.com/pulumi/lumi/pkg/util/cmdutil"
|
||||
"github.com/pulumi/lumi/pkg/util/rpcutil"
|
||||
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Initialize loggers before going any further.
|
||||
cmdutil.InitLogging(false, 0, false)
|
||||
|
||||
// Fire up a gRPC server, letting the kernel choose a free port for us.
|
||||
port, done, err := rpcutil.Serve(0, nil, []func(*grpc.Server) error{
|
||||
func(srv *grpc.Server) error {
|
||||
prov, err := NewProvider()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create AWS resource provider: %v", err)
|
||||
}
|
||||
lumirpc.RegisterResourceProviderServer(srv, prov)
|
||||
return nil
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: %v\n", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
// The resource provider protocol requires that we now write out the port we have chosen to listen on.
|
||||
fmt.Printf("%d\n", port)
|
||||
|
||||
// Finally, wait for the server to stop serving.
|
||||
if err := <-done; err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: %v\n", err)
|
||||
// Create a new resurce provider server and listen for and serve incoming connections.
|
||||
if err := provider.Main(func(host *provider.HostClient) (lumirpc.ResourceProviderServer, error) {
|
||||
return NewProvider(host)
|
||||
}); err != nil {
|
||||
cmdutil.ExitError(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
pbempty "github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/pulumi/lumi/pkg/resource/provider"
|
||||
"github.com/pulumi/lumi/pkg/tokens"
|
||||
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
||||
"golang.org/x/net/context"
|
||||
|
@ -39,8 +40,8 @@ type Provider struct {
|
|||
}
|
||||
|
||||
// NewProvider creates a new provider instance with server objects registered for every resource type.
|
||||
func NewProvider() (*Provider, error) {
|
||||
ctx, err := awsctx.New()
|
||||
func NewProvider(host *provider.HostClient) (*Provider, error) {
|
||||
ctx, err := awsctx.New(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue