How to connect to elasticache in my case? - javascript

I am trying to connect to aws elasticache from my app.
I know the endpoint and the port but for some reason I can't connect to it.
I used this npm package:
https://www.npmjs.com/package/node-memcached-client
code:
const Memcached = require('node-memcached-client');
const client = new Memcached({
host: 'mycache.aa11c.0001.use2.cache.amazonaws.com', //fake aws cache endpoint
port: 11211
});
console.log(client); // I can see it outputs stuff
client.connect()
.then(c => {
console.log('connected');
console.log(c);
}).catch(function(err){
console.log('error connecting');
console.log(err);
});
For some reason when I run the codes, all I see is
[Memcached] INFO: Nothing any connection to mycache.aa11c.0001.use2.cache.amazonaws.com:11211, created sid:1
no errors or connected message in the console.log. Am I doing something wrong here?
Thanks!

You may want to go over the below document from AWS to access Elasticache resources from outside of AWS:
Access AWS Elasticache from outside
I would recommend setting up a local memcached instance for development and debugging, and connect to Elasticache from an EC2 instance in test and production environments.
The ROI for trying to setup NAT and mapping the IP addresses is not justifiable for dev/test unless absolutely necessary.

I ended using port forwarding to bypass the local instance can't connect to elasticache issue.
by using
ssh -L 11211:{your elasticache endpoint}:11211 {your ec2 instance ip}

Related

IOT tcp connection error with nodejs and net

I have created a server using net module:
// Creating and connecting with server
const net = require('net');
const server = net.createServer(); //Creating server
//Connecting with server
server.on('connection', function (socket) {
let remoteAddress = `${socket.remoteAddress},${socket.remotePort}`
console.log(remoteAddress)
console.log(`connection is established... ${Date.now()} \n `);
socket.write(`connection is established...${Date.now()} \n`);
//Receiving and Sending payload from/to client
socket.on('data', async function (payload) {
console.log("payload from client",payload)
socket.write(`acknowledge : ${payload}`);
});
//Close connection
socket.on('close', function () {
console.log('Server Connection Closed');
});
//Server error
socket.on('error', function (err) {
console.log("Caught flash policy server socket error: ")
console.log(err.stack)
});
});
server.listen(8001, function () {
console.log('Server Listing on Port 8001');
})
I deploy this code to AWS EC2 and When I tried to connect with telnet client (telnet ec2_ip 8001), initailly it is working but after sometime it is giving following errors.
screenshot of telnet client:
screenshot of ec2 logs:
And When I tried to connect with real IOT scooter with ec2 Ip address and port 8001, It is not connecting for even a second.
Can anyone please tell me what I am doing wrong?
Note: I don't have much knowledge about IoT. This is the first time I am connecting an IoT scooter with nodejs.
It is hard to know exactly what your problem is without seeing more of your code, or getting more explained. Like what happens in your real code? The logs seem to indicate that you are making outbound calls to another service after the initial connection. If that is the case, are you handling error conditions of your dependencies correctly? An unhandled exception would kill your server, and it would need to be restarted to work again. If you are using dependencies that fail, for instance databases, other servers, configuration files etc that fail on your scooter, that may be why it fails without even allowing one single connection. But without knowing more specifics about your application and architecture, it is just wild guesses at this point.
Your server example code, seems fine. As long as you have opened for traffic on that port in your routing configuration in AWS, it should respond. It could be that you just simply run out of connections, but again that requires me to know more to give you a definitive answer.
My advice would be to check your dependencies, and your logs for supporting systems.

How to host a node js / express server with MySQL on Elastic Beanstalk AWS

I am trying to host my Node js / express server on AWS Elastic Beanstalk. My express server is connected to a MySQL database. So far, I have hosted the MySQL database on an RDS instance on AWS. I have also created an elastic beanstalk environment and "deployed" the Node js server using a CodePipeline (which in connected to GitHub). I have found this process very interesting and feel like I am very close. However, I am running into trouble with the MySQL connection. Here are the relevant lines of code from my Node server:
const express = require("express");
const mysql = require("mysql");
const cors = require("cors")
const formData = require("express-form-data");
const { PORT = 5000 } = process.env
const app = express();
app.use(cors());
app.use(formData.parse({maxFieldSize: '10mb'}));
var mysqlConnection = mysql.createConnection({
host: "my-rds-dbconnection-endpoint",
user: "my-username",
password: "****",
database: "nameOfSchema"
})
mysqlConnection.connect((err) => {
if(err) {
console.log('THIS IS NOT CONNECTING #20')
throw err;
}
console.log("Connected to AWS!")
})
console.log('PORT', PORT)
app.get('/', (request, response) => {
response.send('Hello World!')
console.log('hello from /')
});
app.listen(PORT, function() {
console.log(`App listening on port ${PORT}`);
});
When I run the server using Node in my command prompt for testing purposes, I make it all the way to the message "connected to AWS" and it seems to make the connection to the database. However, when I connect to the environment URL, I get the expected output 'Hello World', but when I download the logs to see what happens, the 'THIS IS NOT CONNECTING' statement prints. Obviously, when I access this node server from the elastic beanstalk environment, it is not connecting to my database. I am unsure why. Does anyone have any ideas about what I could be missing? I sure would appreciate your experience/help!!!
Seeing that you can connect from your local environment and not in your Elastic Beanstalk environment, I assume it's a connectivity issue. There are a few things you can try.
1. RDS Instance - Security Group: Temporarily allow all connections
See if the security group for the RDS instance allows connection from your Elastic Beanstalk environment. You may have whitelisted your own local PC but may not have for other IPs/Security Groups. As an easy check you can allow all connection temporarily to see if this is the issue.
2. RDS Instance - Public Accessibility: Set to Yes/True
For this, I believe it's already configured to True, since you can connect from your local environment with the code provided. Mentioning it here just in case.
3. Elastic Beanstalk - VPC: Set it to be the same as RDS
Connecting EC2(by Elastic Beanstalk) & RDS in different VPC is more complex. Also it makes sense to put them together if you own both environments. After configuring the VPC settings in the Elastic Beanstalk Console, you can set up the security group for the DB and EC2 in the same VPC.
4. Elastic Beanstalk - Security Group: Temporarily allow all connections
While your Elastic Beanstalk environment may accept HTTP/S(TCP Ports 80/443) requests, it may not have been configured for RDS connectivity (TCP Port 3306). Also check if it allows connectivity for your RDS instance (More info.
Note on security: The above two might help get the connection working for now, but is not a proper setting for production environment (not for development either). It's best practice to set RDS public accessibility to No/False. If you haven't planned to already, I recommend setting up a more secure DB environment by SSH Tunnelling using a Bastion Host. Be aware that this will add costs for the EC2 instance, but you can use the free tier instance for the Bastion Host for small projects.

Can't connect to Azure Cache for Redis from Azure Web App

I have NodeJS Web App which trying to connect to the Azure Cache for Redis which is part of the same subscription.
const redis = require('redis')
const redisConnectionConfig = {
host: REDIS_HOST,
port: REDIS_PORT,
auth_pass: REDIS_PASSWORD
tls: { servername: REDIS_HOST }
}
...
redis.createClient(redisConnectionConfig)
I'm able to connect to Redis from my local machine after adding my IP to the Redis Firewall rules.
Also, I've added all 'Outbound IPs and Additional Outbound IP Addresses' from the Service App Properties.
I've tried even to allow access from all IPs
still not pass
But it is not connected and if I try to use Redis I receive the following connection error:
MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
Something similar solved for the VM here. But in the case of the App Service Azure managed that layer.
Looks like it's not connectivity issue.
Network part you always can check via WebApp->Console and use command
tccping redisservername:redisserverport
tcpping_example
Probably something with your redis cache size. What size are you using now?
You can find azure redis limits here

How to use .pem file inside Amazon Lambda to access EC2 instance

I'm currently working on a project that take place inside the AWS environment. I have configure a S3 bucket in order to receive mails (mails are coming from SES but that's not relevant).
What I want to do is to create a Lambda function that will be able to access a EC2 instance and launch a python scripts. So far i have the code below. The problem is that when I created my ec2 instance, I didnt create any username or password to connect via SSH. I only have a .pem file (certificate file) to authenticate to the instance.
I did some research but i couldn't find anything useful.
var SSH = require('simple-ssh');
var ssh = new SSH({
host: 'localhost',
user: 'username',
pass: 'password'
});
ssh.exec('python3.6 path/to/my/python/script.py', {
out: function(stdout) {
console.log(stdout);
}
}).start();
i've been thinking of severals solutions, but i'm not sure at all :
find an SSH library in Javascript that handle .pem file
converting .pem into a String (not secure at all, in my opinion).
maybe create a new ssh user in EC2 ?
Thanks you for your time.
A better option would be to use AWS Systems Manager to remotely run commands on your Amazon EC2 instances.
If you still choose to use simple-ssh then you need to supply an SSH key in config.key when creating your SSH object. You can store the private key in Parameter Store or Secrets Manager and retrieve it within the Lambda. In this case, you should definitely use passwordless SSH (with keypair).

Connecting to Named Unix Domain Socket Node

I am trying to connect to a named Unix Domain Socket via nodejs. I have seen that the docs seem to support connecting to Unix Sockets, however I haven't seen any examples of connecting to a socket by name, and not by accessing a socket file at a well known location.
I can clearly see that the socket I need to connect to is being created by using lsof (and some grepping):
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
run 5632 user 3u unix 0xffff8803dd4b6000 0t0 29647 ##user#5632#1 type=SEQPACKET
The name of the socket is being passed to the script that actually runs my node script. I have tried the following:
import net = require('net');
var socket;
var element = "#user#5632#1"; //Parsed from args
try {
socket = net.createConnection("#"+element,(error)=>{
if(error){
console.log(error)
}else{
console.log("Connection Established "+element);}
});
socket.on('error', function(err) {
console.log("Error: " + err);
});
} catch (error) {
console.error(error);
}
Clearly, I have this wrapped up to catch errors in a few places(at different points in execution), but the point is that createConnection is throwing:
Error: Error: connect ENOENT ##user#5882#1
Using net.connectcauses the exact same error.
I tested creating a socket (file) in a well known location, and connecting to is, and that worked just fine, but as far as I have determined, node, or at least the net module does not seem to support connecting to ephemeral sockets.
Anybody know if there is a way to do this, or if I need to format my socket name differently in order to connect or any help really?
Node does not support SEQPACKET sockets. You may submit a PR to the node issue tracker as a feature request to add support and/or you may need to write a node addon that lets you connect to SEQPACKET sockets.

Categories