This is a proxy server on AWS using Lambda and API Gateway. It is a simple proxy server that proxies a website and returns the response to the client.
- You will need to have NodeJS installed on your machine. You can download it from here.
- You will need to have an AWS account. You can sign up for one here.
- You will need tohave an AWS IAM user with programmatic access. You can create one here.
- You will need to instal the AWS CLI on your machine. You can download it from here. (Mandatory for deployment)
- Clone this repository to your local machine using
git clone https://github.com/ifeLight/proxy-server-aws-lambda.git proxy-server-aws-lambda
- Change into the directory using
cd proxy-server-aws-lambda
- Install the dependencies using
npm install
- Copy the
config.sample.json
file tosrc/config.json
usingcp config.sample.json src/config.json
- Open the
config.json
and and changehttps://www.google.com/
to the URL of the website you want to proxy. - Run the server using
npm start
. - Open your browser and navigate to
http://localhost:8080/
to see the website you are proxying.
To run the tests, run npm test
. If no errors are displayed, then the tests have passed.
- Before deployment you will need to have an AWS IAM user with programmatic access. You can create one here.
- The user has to have Full Access to AWS Lambda and API Gateway and most especially the execution role of the Lambda function. Here is a guide on how to create an execution role for a Lambda function.
- Ensure you have added
AWSXRayDaemonWriteAccess
andAWSLambdaBasicExecutionRole
permissions. - You will need to have the AWS CLI installed on your machine. You can download it from here. (Mandatory for deployment)
- The copy the
.aws/sample-aws-credentials
to the home directory usingcp .aws/sample-aws-credentials ~/.aws/credentials
- Open the
~/.aws/credentials
file and replace theaws_access_key_id
andaws_secret_access_key
with the access key ID and secret access key of the IAM user you created in step 1.
To deploy the server to AWS Lambda, you will run on your terminal at the home directory of the project:
aws lambda create-function --function-name <<the function name>> --runtime "nodejs16.x" --role <<the role>> --zip-file "fileb://dist/index.zip" --handler index.handler --region <<the region>>
Replace:
<<the role>>
with the ARN of the execution role you created in step 2 of the prerequisites. The ARN looks like thisarn:aws:iam::123456789012:role/lambda-ex
, for more information on how to get the ARN of the execution role, see here.s<<the region>>
with the region you want to deploy to. (e.g.us-east-1
,us-west-2
,eu-central-1
etc.)<<the function name>>
with the name you want to give the function. (e.g.proxy-server
)
You will get a response simillar to this:
{
"FunctionName": "proxy-server2",
"FunctionArn": "arn:aws:lambda:eu-central-1:965800799013:function:proxy-server2",
"Runtime": "nodejs16.x",
"Role": "arn:aws:iam::965800799013:role/service-role/proxy-server-role-w1rt0ybq",
"Handler": "index.handler",
"CodeSize": 860277,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2023-06-08T12:55:20.009+0000",
"CodeSha256": "28/ir181RyRq3SujMEe37Fw/xjRH1jGH6vf6a3DfC4I=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "d418c63d-b019-4dba-818e-91a4cd2da383",
"State": "Pending",
"StateReason": "The function is being created.",
"StateReasonCode": "Creating",
"PackageType": "Zip",
"Architectures": [
"x86_64"
],
"EphemeralStorage": {
"Size": 512
},
"SnapStart": {
"ApplyOn": "None",
"OptimizationStatus": "Off"
}
}
Take note of the FunctionArn
and FunctionName
as you will need them in the next step.
To get the Function url, you will run on your terminal at the home directory of the project:
aws lambda create-function-url-config --function-name <<the function name>> --auth-type NONE --cors '{"AllowOrigins":["*"], "AllowHeaders": ["*"], "AllowMethods": ["*"]}' --region <<the region>>
Replace:
<<the region>>
with the region you want to deploy to. (e.g.us-east-1
,us-west-2
,eu-central-1
etc.)<<the function name>>
with the name you gave the function in the previous step. (e.g.proxy-server
)
This will create a lambad function url and with a repsonse similar to this:
{
"FunctionUrl": "https://ckycrb5ojxgjfepz7mg3cmfki40nlqbk.lambda-url.us-east-1.on.aws/",
"FunctionArn": "arn:aws:lambda:us-east-1:965800799013:function:proxy-server",
"AuthType": "NONE",
"CreationTime": "2023-06-08T13:20:18.244654Z"
}
The url is the FunctionUrl
in the response above. But you have to give public access to the function url. To do this, you will run on your terminal at the home directory of the project:
aws lambda add-permission --function-name <<the function name>> --action lambda:invokeFunctionUrl --principal "*" --output text --statement-id sample0-statement --region <<the region>> --function-url-auth-type NONE
Replace:
<<the region>>
with the region you want to deploy to. (e.g.us-east-1
,us-west-2
,eu-central-1
etc.)<<the function name>>
with the name you gave the function in the previous step. (e.g.proxy-server
)
If successful, you will get a response similar to this:
{"Sid":"sample0-statement","Effect":"Allow","Principal":"*","Action":"lambda:invokeFunctionUrl","Resource":"arn:aws:lambda:eu-central-1:965800799013:function:proxy-server3","Condition":{"StringEquals":{"lambda:FunctionUrlAuthType":"NONE"}}}
After all, you can now access the website using the function url. (e.g. https://ckycrb5ojxgjfepz7mg3cmfki40nlqbk.lambda-url.us-east-1.on.aws/
)
For a faster way to deploy, you can use visit the Deploy readme.