Your Amazon ElastiCache for Valkey datastores are designed to be accessed through an Amazon EC2 instance. You can access your ElastiCache for Valkey datastores from an Amazon EC2 instance in the same Amazon VPC, or by using VPC peering, you can access your ElastiCache for Valkey datastores from an Amazon EC2 in a different Amazon VPC.
The following instructions will help you create an EC2 instance in the same VPC as your ElastiCache for Valkey datastore, and will guide you to configure the security groups required to access the cache from your desktop through an SSH tunnel.
Complete the following steps:
- Open the Amazon EC2 console, and then choose Launch instance.
- Select an Amazon Machine Image (AMI).
- Choose an instance type, and then choose Next: Configure Instance Details.
- For Network, choose the VPC that the Amazon ElastiCache Valkey cache uses.
- For Subnet, select the private subnet in the VPC
- Choose Next: Add Storage, and then modify the storage as needed.
- Choose Next: Add Tags, and then add tags as needed.
- Choose Next: Configure Security Group.
- Choose Add Rule, and then enter the following:
- For Type, enter Custom TCP Rule
- For Protocol, enter TCP
- For Port Range, enter 22
- For Source, enter the security group used by your Amazon EC2 connect endpoint.
- Choose Review and Launch, and then choose Launch.
Complete the following steps:
- Open the Amazon ElastiCache console.
- In the navigation pane, choose Resources → Valkey caches.
- Choose the name of the Amazon Valkey Cache. If you don't already have one, then create it.
- Under Actions, select the option “Setup compute connection - new”
- In the dropdown, select the EC2 instance you created above.
- Click Setup.
This configuration for the security group allows traffic from the EC2 instance's private IP address. If the EC2 instance and the Amazon ElastiCache Valkey cache use the same VPC, then you don't need to modify the Amazon ElastiCache Valkey cache route table. If the VPC is different, then create a VPC peering connection to allow connections between those VPCs. Note: If you use a more scalable solution, then review your configuration. For example, if you use the security group ID in a security group rule, then make sure that it doesn't restrict access to one instance. Instead, configure the rule to restrict access to any resource that uses the specific security group ID.
- Open the Amazon VPC console.
- In the navigation pane, choose Endpoints.
- Choose Create endpoint, and then specify the endpoint settings.
- (Optional) For Name tag, enter a name for the endpoint.
- For Service category, choose EC2 Instance Connect Endpoint.
- For VPC, select the VPC that has the target instances.
- (Optional) To preserve client IP addresses, expand Additional settings and select the check box. Otherwise, the default is to use the endpoint network interface as the client IP address.
- For Security groups, select the security group you want to associate with the endpoint. Otherwise, the default is to use the default security group for the VPC.
- For Subnet, select the subnet in which to create the endpoint.
- (Optional) To add a tag, choose Add new tag and enter the tag key and the tag value.
- Review your settings and then choose Create endpoint.
- The initial status of the endpoint is Pending. To connect to an instance, you must wait until the endpoint status is Available. This can take up to a few minutes.
Note: You must have access to the AWS CLI.
To connect from your local MCP Server to a private Amazon ElastiCache Valkey cache through an SSH tunnel, complete the following steps: Linux or macOS Run the following command to open a tunnel from local machine to the EC2 instance:
aws ec2-instance-connect open-tunnel --instance-id ec2-instance-ID --local-port 6379
Note: Replace ec2-instance-ID with your EC2 instance ID.
Open a second connection and run the following command to create an SSH tunnel from your local host to your ElastiCache Valkey Cache through an EC2 instance:
ssh -i YOUR_EC2_KEY EC2_USER@EC2_HOST -p EC2_TUNNEL_PORT -L LOCAL_PORT:ELASTICACHE_ENDPOINT:REMOTE_PORT -N -f
Note: Replace the following values:
- YOUR_EC2_KEY with the path to your EC2 private key file
- EC2_USER with your EC2 instance username
- EC2_HOST with the hostname of your EC2 instance
- EC2_TUNNEL_PORT with the port you configured
- LOCAL_PORT with an unused port on your local machine (6379)
- ELASTICACHE_ENDPOINT with the endpoint of your ElastiCache Valkey cache
- REMOTE_PORT with the port that your Amazon ElastiCache Valkey cache uses (6379)
Use a third connection and run the following command to verify connection to your Amazon ElastiCache Valkey cache from your local machine:
valkey-cli -h 127.0.0.1 -p LOCAL_PORT
Note: Replace the following values:
- LOCAL_PORT with the number of your local port (6379)