Skip to content

Commit 0d626a5

Browse files
committed
Add pulumi.Asset support to lambda function
1 parent 7fd9e0d commit 0d626a5

File tree

21 files changed

+173
-69
lines changed

21 files changed

+173
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (Unreleased)
22

3+
- Add lambda.Function support for pulumi Assets [#182](https://github.com/pulumi/pulumi-aws-native/pull/182)
4+
35
## 0.2.0 (October 8, 2021)
46

57
- Deduplicate type names [#160](https://github.com/pulumi/pulumi-aws-native/issues/160)

examples/aws-native-ts-stepfunctions/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ const worldFunction = new awsnative.lambda.Function('worldFunction',
4545
runtime: "nodejs14.x",
4646
handler: "index.handler",
4747
code: {
48-
zipFile: `exports.handler = function(event, context, callback){
49-
var response = event.response;
50-
const updated = { "response": response + "World!" };
51-
callback(null, updated);
52-
};`,
48+
zipFile: new pulumi.asset.FileAsset("world_function.js"),
5349
},
5450
}, {dependsOn: lambdaRolePolicy});
5551

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.handler = function(event, context, callback){
2+
var response = event.response;
3+
const updated = { "response": response + "World!" };
4+
callback(null, updated);
5+
};

examples/simple-ts/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import * as aws from "@pulumi/aws-native";
44
import * as random from "@pulumi/random";
5+
import * as pulumi from "@pulumi/pulumi";
6+
7+
new aws.lambda.Function("test", {
8+
code: {
9+
zipFile: new pulumi.asset.FileAsset("lambda_function_payload.js"),
10+
},
11+
});
512

613
const name = new random.RandomString("name", {
714
length: 8,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var aws = require('aws-sdk')
2+
var response = require('cfn-response')
3+
exports.handler = function(event, context) {
4+
console.log("REQUEST RECEIVED:\\n" + JSON.stringify(event))
5+
// For Delete requests, immediately send a SUCCESS response.
6+
if (event.RequestType == "Delete") {
7+
response.send(event, context, "SUCCESS")
8+
return
9+
}
10+
var responseStatus = "FAILED"
11+
var responseData = {}
12+
var functionName = event.ResourceProperties.FunctionName
13+
var lambda = new aws.Lambda()
14+
lambda.invoke({ FunctionName: functionName }, function(err, invokeResult) {
15+
if (err) {
16+
responseData = {Error: "Invoke call failed"}
17+
console.log(responseData.Error + ":\\n", err)
18+
}
19+
else responseStatus = "SUCCESS"
20+
response.send(event, context, responseStatus, responseData)
21+
})
22+
}

provider/cmd/cf2pulumi/schema-full.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44626,7 +44626,7 @@
4462644626
"description": "For versioned objects, the version of the deployment package object to use."
4462744627
},
4462844628
"zipFile": {
44629-
"type": "string",
44629+
"$ref": "pulumi.json#/Asset",
4463044630
"description": "The source code of your Lambda function. If you include your function source inline with this parameter, AWS CloudFormation places it in a file named index and zips it to create a deployment package.."
4463144631
}
4463244632
},

provider/cmd/pulumi-resource-aws-native/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44212,7 +44212,7 @@
4421244212
"description": "For versioned objects, the version of the deployment package object to use."
4421344213
},
4421444214
"zipFile": {
44215-
"type": "string",
44215+
"$ref": "pulumi.json#/Asset",
4421644216
"description": "The source code of your Lambda function. If you include your function source inline with this parameter, AWS CloudFormation places it in a file named index and zips it to create a deployment package.."
4421744217
}
4421844218
}

provider/cmd/pulumi-resource-aws-native/schema.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

provider/go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,9 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
485485
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
486486
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
487487
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
488-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
489488
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
489+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
490+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
490491
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
491492
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
492493
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=

provider/pkg/provider/provider.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (p *cfnProvider) DiffConfig(ctx context.Context, req *pulumirpc.DiffRequest
235235
news, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
236236
Label: fmt.Sprintf("%s.news", label),
237237
KeepUnknowns: true,
238-
RejectAssets: true,
238+
RejectAssets: false,
239239
})
240240
if err != nil {
241241
return nil, errors.Wrapf(err, "diffConfig failed because of malformed resource inputs")
@@ -511,7 +511,7 @@ func (p *cfnProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (*
511511
newInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
512512
Label: fmt.Sprintf("%s.properties", label),
513513
KeepUnknowns: true,
514-
RejectAssets: true,
514+
RejectAssets: false,
515515
KeepSecrets: true,
516516
})
517517
if err != nil {
@@ -579,7 +579,7 @@ func (p *cfnProvider) Create(ctx context.Context, req *pulumirpc.CreateRequest)
579579
inputs, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
580580
Label: fmt.Sprintf("%s.properties", label),
581581
KeepUnknowns: true,
582-
RejectAssets: true,
582+
RejectAssets: false,
583583
KeepSecrets: true,
584584
})
585585
if err != nil {
@@ -589,6 +589,7 @@ func (p *cfnProvider) Create(ctx context.Context, req *pulumirpc.CreateRequest)
589589
resourceToken := string(urn.Type())
590590
var cfType string
591591
var payload map[string]interface{}
592+
//q.Q(resourceToken) // DEBUG
592593
switch resourceToken {
593594
case schema.ExtensionResourceToken:
594595
// For a custom resource, both CF type and inputs shape are defined explicitly in the SDK.
@@ -611,7 +612,10 @@ func (p *cfnProvider) Create(ctx context.Context, req *pulumirpc.CreateRequest)
611612
cfType = spec.CfType
612613

613614
// Convert SDK inputs to CFN payload.
614-
payload = schema.SdkToCfn(&spec, p.resourceMap.Types, inputs.MapRepl(nil, mapReplStripSecrets))
615+
payload, err = schema.SdkToCfn(&spec, p.resourceMap.Types, inputs.MapRepl(nil, mapReplStripSecrets))
616+
if err != nil {
617+
return nil, fmt.Errorf("failed to convert SDK inputs to CFN: %w", err)
618+
}
615619
}
616620

617621
// Serialize inputs as a desired state JSON.
@@ -814,7 +818,7 @@ func (p *cfnProvider) Update(ctx context.Context, req *pulumirpc.UpdateRequest)
814818
newInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
815819
Label: fmt.Sprintf("%s.newInputs", label),
816820
KeepUnknowns: true,
817-
RejectAssets: true,
821+
RejectAssets: false,
818822
KeepSecrets: true,
819823
})
820824
if err != nil {
@@ -1001,7 +1005,7 @@ func (p *cfnProvider) diffState(olds *pbstruct.Struct, news *pbstruct.Struct, la
10011005
oldState, err := plugin.UnmarshalProperties(olds, plugin.MarshalOptions{
10021006
Label: fmt.Sprintf("%s.oldState", label),
10031007
KeepUnknowns: true,
1004-
RejectAssets: true,
1008+
RejectAssets: false,
10051009
KeepSecrets: true,
10061010
})
10071011
if err != nil {
@@ -1014,7 +1018,7 @@ func (p *cfnProvider) diffState(olds *pbstruct.Struct, news *pbstruct.Struct, la
10141018
newInputs, err := plugin.UnmarshalProperties(news, plugin.MarshalOptions{
10151019
Label: fmt.Sprintf("%s.newInputs", label),
10161020
KeepUnknowns: true,
1017-
RejectAssets: true,
1021+
RejectAssets: false,
10181022
KeepSecrets: true,
10191023
})
10201024
if err != nil {

0 commit comments

Comments
 (0)