Skip to content

Commit

Permalink
fix: aws duplicated bucketname & default path (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lellansin committed Jul 28, 2020
1 parent c78fdfd commit 28b6520
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@
"MemorySize": <%- opt.memorySize || 128 %>,
"Timeout": <%= opt.timeout || 3 %>,
"Role": {
"Fn::GetAtt": ["InvokerRole", "Arn"],
"Fn::GetAtt": ["InvokerRole", "Arn"]
},
"CodeUri": {
"Bucket": "<%= opt.codeBucket %>",
"Key": "<%= opt.codeKey %>"
},
"Events": {
<% opt.events.forEach(function (event, idx) { %>
"Api<%= idx + opt.name + 1 %>": {
"Api<%= idx + opt.logicId + 1 %>": {
"Type": "Api",
"Properties": {
"Path": "<%= event.path %>",
"Path": "<%= event.path || '/*' %>",
"Method": "<%= event.method || 'ANY' %>",
"RestApiId": {
"Ref": "MyApi"
Expand Down
33 changes: 29 additions & 4 deletions packages/faas-cli-plugin-aws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { homedir } from 'os';
import { join, basename } from 'path';
import {
readFileSync,
createReadStream,
writeFileSync,
createReadStream,
mkdirSync,
existsSync,
} from 'fs';
import { S3, Lambda, CloudFormation } from 'aws-sdk';
import { S3, Lambda, CloudFormation, IAM, MetadataService } from 'aws-sdk';
import { render } from 'ejs';
import { writeWrapper } from '@midwayjs/serverless-spec-builder';
import { BasePlugin, ICoreInstance } from '@midwayjs/fcli-command-core';
Expand Down Expand Up @@ -169,7 +169,10 @@ export class AWSLambdaPlugin extends BasePlugin {
stage,
},
};
return render(tpl, params);
const text = render(tpl, params);
const tmpJSONPath = join(this.midwayBuildPath, 'template.json');
writeFileSync(tmpJSONPath, text);
return text;
}

async createStack(
Expand Down Expand Up @@ -408,6 +411,27 @@ export class AWSLambdaPlugin extends BasePlugin {
}));
}

async getAccount() {
const iam = new IAM();
const metadata = new MetadataService();

return new Promise((resolve, reject) => {
iam.getUser({}, (err, data) => {
if (err) {
metadata.request('/latest/meta-data/iam/info/', (err, data) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(data).InstanceProfileArn.split(':')[4]);
}
});
} else {
resolve(data.User.Arn.split(':')[4]);
}
});
});
}

async deploy() {
const stage = 'v1';
const fns = this.getFunctions();
Expand Down Expand Up @@ -446,8 +470,9 @@ export class AWSLambdaPlugin extends BasePlugin {
credentials = this.getCredentials(true);
}
credentials.region = this.getRegion();
const accountId = await this.getAccount();

const bucket = `${this.core.service.service.name}-deploymentbucket`;
const bucket = `${accountId}-${this.core.service.service.name}-deploymentbucket`;
await this.featchBucket(bucket);
const artifactRes = await this.uploadArtifact(bucket);
// console.log('artifactRes', artifactRes);
Expand Down

0 comments on commit 28b6520

Please sign in to comment.