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
3 changed files with 26 additions and 34 deletions
|
@ -18,6 +18,7 @@ package awsctx
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/apigateway"
|
"github.com/aws/aws-sdk-go/service/apigateway"
|
||||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
"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/lambda"
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
"github.com/golang/glog"
|
"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/pkg/util/contract"
|
||||||
|
|
||||||
"github.com/pulumi/lumi/lib/aws/provider/arn"
|
"github.com/pulumi/lumi/lib/aws/provider/arn"
|
||||||
|
@ -50,13 +53,25 @@ type Context struct {
|
||||||
s3 *s3.S3
|
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.
|
// 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")
|
glog.V(5).Infof("Creating a new AWS session object w/ default credentials")
|
||||||
// IDEA: consider verifying credentials, region, etc. here.
|
// IDEA: consider verifying credentials, region, etc. here.
|
||||||
// IDEA: currently we just inherit the standard AWS SDK credentials logic; eventually we will want more
|
// 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.
|
// 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,40 +16,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/pulumi/lumi/pkg/resource/provider"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/pulumi/lumi/pkg/util/cmdutil"
|
"github.com/pulumi/lumi/pkg/util/cmdutil"
|
||||||
"github.com/pulumi/lumi/pkg/util/rpcutil"
|
|
||||||
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
||||||
"google.golang.org/grpc"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize loggers before going any further.
|
// Create a new resurce provider server and listen for and serve incoming connections.
|
||||||
cmdutil.InitLogging(false, 0, false)
|
if err := provider.Main(func(host *provider.HostClient) (lumirpc.ResourceProviderServer, error) {
|
||||||
|
return NewProvider(host)
|
||||||
// Fire up a gRPC server, letting the kernel choose a free port for us.
|
}); err != nil {
|
||||||
port, done, err := rpcutil.Serve(0, nil, []func(*grpc.Server) error{
|
cmdutil.ExitError(err.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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
pbempty "github.com/golang/protobuf/ptypes/empty"
|
pbempty "github.com/golang/protobuf/ptypes/empty"
|
||||||
|
"github.com/pulumi/lumi/pkg/resource/provider"
|
||||||
"github.com/pulumi/lumi/pkg/tokens"
|
"github.com/pulumi/lumi/pkg/tokens"
|
||||||
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
"github.com/pulumi/lumi/sdk/go/pkg/lumirpc"
|
||||||
"golang.org/x/net/context"
|
"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.
|
// NewProvider creates a new provider instance with server objects registered for every resource type.
|
||||||
func NewProvider() (*Provider, error) {
|
func NewProvider(host *provider.HostClient) (*Provider, error) {
|
||||||
ctx, err := awsctx.New()
|
ctx, err := awsctx.New(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue