Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions WorkingCFN.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
Description: The ID for the VPC in which the EC2 instance will be launched.

Subnet:
Type: AWS::EC2::Subnet::Id
Description: The ID for the Subnet in which the EC2 instance will be launched.

Resources:
SampleInstanceProfileRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ''
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier
XRayWriteOnlyPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: AWSXRayWriteOnlyAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sns:Publish
- xray:PutTelemetryRecords
- xray:PutTraceSegments
- dynamodb:PutItem
Resource:
- "*"
Roles:
- Ref: SampleInstanceProfileRole
SampleInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: SampleInstanceProfileRole
SampleEBServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: elasticbeanstalk.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
sts:ExternalId: elasticbeanstalk
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService
- arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth
ElasticBeanstalkApplication:
Type: AWS::ElasticBeanstalk::Application
Properties:
ApplicationName:
Ref: AWS::StackName
ElasticBeanstalkApplicationVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName:
Ref: ElasticBeanstalkApplication
SourceBundle:
S3Bucket:
Fn::Join:
- "."
- - aws-xray-assets
- Ref: AWS::Region
S3Key: samples/aws-xray-node-sample-app.zip
ElasticBeanstalkEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName:
Ref: ElasticBeanstalkApplication
Description: AWS X-Ray Getting Started Sample Application
EnvironmentName:
Ref: AWS::StackName
OptionSettings:
- Namespace: aws:autoscaling:launchconfiguration
OptionName: InstanceType
Value: t3.large
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value:
Ref: SampleInstanceProfile
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value:
Ref: SampleEBServiceRole
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: SingleInstance
- Namespace: aws:elasticbeanstalk:healthreporting:system
OptionName: SystemType
Value: enhanced
-
Namespace: "aws:ec2:vpc"
OptionName: AssociatePublicIpAddress
Value: true
-
Namespace: "aws:ec2:vpc"
OptionName: ELBSubnets
Value: !Ref Subnet
-
Namespace: "aws:ec2:vpc"
OptionName: Subnets
Value: !Ref Subnet
-
Namespace: "aws:ec2:vpc"
OptionName: VPCId
Value: !Ref VPC
PlatformArn:
Fn::Join:
- ":"
- - arn:aws:elasticbeanstalk
- Ref: AWS::Region
- :platform/Node.js 16 running on 64bit Amazon Linux 2/5.6.2
VersionLabel:
Ref: ElasticBeanstalkApplicationVersion
Outputs:
ElasticBeanstalkEnvironmentURL:
Description: URL for the Elastic Beanstalk Getting Started Sample Application
Value:
Fn::GetAtt:
- ElasticBeanstalkEnvironment
- EndpointURL
SampleInstanceProfileRole:
Description: IAM Role used for AWS X-Ray Getting Started Sample Application
Value:
Fn::GetAtt:
- SampleInstanceProfileRole
- Arn
SampleInstanceProfile:
Description: Instance Profile used for AWS X-Ray Getting Started Sample Application
Value:
Ref: SampleInstanceProfile
SampleEBServiceRole:
Description: IAM Role used for AWS Elastic Beanstalk Service Role
Value:
Fn::GetAtt:
- SampleEBServiceRole
- Arn
94 changes: 18 additions & 76 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,29 @@ if (cluster.isMaster) {

// Code to run if we're in a worker process
} else {
// Include the AWS X-Ray Node.js SDK and set configuration
var XRay = require('aws-xray-sdk');
var AWS = XRay.captureAWS(require('aws-sdk'));
var http = XRay.captureHTTPs(require('http'));
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
var queryString = require('querystring');

AWS.config.region = process.env.REGION

XRay.config([XRay.plugins.EC2Plugin, XRay.plugins.ElasticBeanstalkPlugin]);
XRay.middleware.setSamplingRules('sampling-rules.json');
XRay.middleware.enableDynamicNaming();

var app = express();
var sns = new AWS.SNS();
var ddb = new AWS.DynamoDB();
var ddbTable = process.env.STARTUP_SIGNUP_TABLE;
var snsTopic = process.env.NEW_SIGNUP_TOPIC;
var apiCNAME = process.env.API_CNAME || 'localhost';

var ddbTable = process.env.STARTUP_SIGNUP_TABLE;
var snsTopic = process.env.NEW_SIGNUP_TOPIC;
var app = express();

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({extended:false}));
app.use(XRay.express.openSegment('myfrontend'));

app.get('/', function(req, res) {
XRay.captureAsyncFunc('Page Render', function(seg) {
res.render('index', {
static_path: 'static',
theme: process.env.THEME || 'flatly',
flask_debug: process.env.FLASK_DEBUG || 'false'
});
seg.close();
res.render('index', {
static_path: 'static',
theme: process.env.THEME || 'flatly',
flask_debug: process.env.FLASK_DEBUG || 'false'
});

res.status(200).end();
});

app.post('/signup', function(req, res) {
Expand All @@ -70,22 +56,20 @@ if (cluster.isMaster) {
'theme': {'S': req.body.theme}
};

var seg = XRay.getSegment();
seg.addAnnotation('email', req.body.email);
seg.addAnnotation('theme', req.body.theme);
seg.addAnnotation('previewAccess', req.body.previewAccess);

ddb.putItem({
'TableName': ddbTable,
'Item': item,
'Expected': { email: { Exists: false } }
}, function(err, data) {
if (err) {
var returnStatus = 500;

if (err.code === 'ConditionalCheckFailedException') {
res.status(409).end("User already exists");
} else {
res.status(500).end("DDB Error");
returnStatus = 409;
}

res.status(returnStatus).end();
console.log('DDB Error: ' + err);
} else {
sns.publish({
'Message': 'Name: ' + req.body.name + "\r\nEmail: " + req.body.email
Expand All @@ -95,58 +79,16 @@ if (cluster.isMaster) {
'TopicArn': snsTopic
}, function(err, data) {
if (err) {
res.status(500).end("SNS Error");
res.status(500).end();
console.log('SNS Error: ' + err);
} else {
res.status(201).end("Success");
res.status(201).end();
}
});
}
});
});

app.post('/remoteSignup', function(req, res) {
var seg = XRay.getSegment();
seg.addAnnotation('theme', req.body.theme);
seg.addAnnotation('previewAccess', req.body.previewAccess);

var reqData = queryString.stringify(req.body);

var options = {
host: apiCNAME,
port: '80',
path: '/signup',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(reqData)
}
};

// Set up the request
var remoteReq = http.request(options, function(remoteRes) {
var body = '';
remoteRes.setEncoding('utf8');

remoteRes.on('data', function(chunk) {
body += chunk;
});

remoteRes.on('end', function() {
res.status(remoteRes.statusCode).send(body);
});
});

remoteReq.on('error', function(err) {
res.status(500).end("Remote error");
});

// post the data
remoteReq.write(reqData);
remoteReq.end();
});

app.use(XRay.express.closeSegment());

var port = process.env.PORT || 3000;

var server = app.listen(port, function () {
Expand Down
18 changes: 3 additions & 15 deletions iam_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:PutItem"
],
"Action": [ "dynamodb:PutItem" ],
"Resource": [ "*" ]
},
{
"Effect": "Allow",
"Action": [
"sns:Publish"
],
"Action": [ "sns:Publish" ],
"Resource": [ "*" ]
},
{
"Effect":"Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords"
],
"Resource":[ "*" ]
}
]
}
}
Loading