About this question
I am a DevOps engineer and I am currently managing applications on an AWS EC2 Instance. In need to upload w Configuration file to an EC2 Instance securely. How can I do so?
I am a DevOps engineer and I am currently managing applications on an AWS EC2 Instance. In need to upload w Configuration file to an EC2 Instance securely. How can I do so?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask AWS Expert
Answered on Apr 24, 2024
In the context of AWS, here are the appropriate approaches given by using the example of Python coding snippet using paramiko, an SSH Library:-
Import paramiko
# Define EC2 instance details
Ec2_instance_ip = ‘EC2_INSTANCE_IP’
Username = ‘ec2-user’ # Replace with the appropriate username
Private_key_path = ‘PATH_TO_PRIVATE_KEY_FILE’
# Create SSH client
Ssh_client = paramiko.SSHClient()
Ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Load private key for authentication
Private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
# Connect to the EC2 instance
Ssh_client.connect(hostname=ec2_instance_ip, username=username, pkey=private_key)
# Upload file to EC2 instance
Local_file_path = ‘PATH_TO_LOCAL_FILE’
Remote_file_path = ‘PATH_TO_REMOTE_DIRECTORY/FILE_NAME’
Sftp_client = ssh_client.open_sftp()
Sftp_client.put(local_file_path, remote_file_path)
# Close SSH and SFTP connections
Sftp_client.close()
Ssh_client.close()
Print(f”File uploaded successfully to {ec2_instance_ip}:{remote_file_path}”)Below is the example given of java programming language by using the JSch library for uploading a file to an EC2 Instance securely by using the SSH protocol:-
Import com.jcraft.jsch.Channel;
Import com.jcraft.jsch.ChannelSftp;
Import com.jcraft.jsch.JSch;
Import com.jcraft.jsch.Session;
Import java.io.File;Public class SshFileUploader {
Public static void main(String[] args) {
String ec2InstanceIp = “EC2_INSTANCE_IP”;
String username = “ec2-user”; // Replace with the appropriate username
String privateKeyPath = “PATH_TO_PRIVATE_KEY_FILE”;
String localFilePath = “PATH_TO_LOCAL_FILE”;
String remoteDirectory = “PATH_TO_REMOTE_DIRECTORY”;
String fileName = “FILE_NAME”;
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
Try {
// Load private key
Jsch.addIdentity(privateKeyPath);
// Establish SSH session
Session = jsch.getSession(username, ec2InstanceIp, 22);
Session.setConfig(“StrictHostKeyChecking”, “no”);
Session.connect();
// Open SFTP channel
Channel = session.openChannel(“sftp”);
Channel.connect();
channelSftp = (ChannelSftp) channel;
// Change directory to remote directory
channelSftp.cd(remoteDirectory);
// Upload file
File file = new File(localFilePath);
channelSftp.put(file.getAbsolutePath(), fileName);
System.out.println(“File uploaded successfully.”);
} catch (Exception e) {
e.printStackTrace();
} finally {
If (channelSftp != null) {
channelSftp.disconnect();
}
If (channel != null) {
Channel.disconnect();
}
If (session != null) {
Session.disconnect();
}
}
}
}Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step AWS guides from industry experts
Common AWS interview questions, answered
Guides, tips and career advice on AWS from JanBask experts.
AWS How to Become an AWS Certified Solutions Architect
Learn how to become an AWS Solutions Architect with our complete guide. Discover exam requirements, salary potential, and…
AWS AWS Solution Architect Salary in 2025 by Location, Skills, and Experience
The AWS Solution Architect Salary varies as per location, job role, skills, and more. Read this blog to learn how much an AWS…
AWS AWS Certification Cost, Courses, Exam Details, Path & Salary : The Comprehensive Guide
How Much Does AWS Certification Cost In 2025- Get aws certification price, types, and AWS certification course fees for your…
AWS Why AWS Training Certification is a Game-Changer for Your Career in 2025
Discover why AWS training certification is crucial in 2025. Learn how it boosts career opportunities, skills, and salary…