Skip to content

Latest commit

 

History

History
179 lines (115 loc) · 9.16 KB

README.md

File metadata and controls

179 lines (115 loc) · 9.16 KB

myb-nicehash-withdrawal

An AWS Lambda function to automate withdrawals from Nice Hash

This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI.

The template.yaml is a template that defines the application's AWS resources and configurations for those resources. Pay special attention to this file for configuring the project.

If you prefer to use an integrated development environment (IDE) to build and test the application, you can use the AWS Toolkit.
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.

AWS SAM CLI

The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run the functions in an Amazon Linux environment that matches Lambda.

To use the SAM CLI, you need the following tools.

Nice Hash

This project interacts with the NiceHash API. In order to do so, you need an account with them and API key.

Nice Hash has documentation on how to get started using their API here.

Note that for withdrawals, the API key you create will need Withdraw funds (WIFU) permissions.

They have a repository for demo REST clients on GitHub here.

A withdrawal address is also required. You can view your addresses here. If you know your NiceHash internal address ID, you can use that directly. If you do not know what that is, you should use the address type and address. For Coinbase, address type is simply COINBASE and address is your coinbase account email. For bitcoin withdrawal addresses, you would use an address type of BITGO and the bitcoin address.

Set up Secrets

This project utilizes a combination of in-template environment variable configuration settings, as well as referenced plaintext strings using AWS Systems Manager

To create secrets using the AWS CLI, follow the instructions here. You will need to populate secrets based on the referenced names in the template.yaml and make sure to use the same region as specified in the samconfig.toml.

The general syntax of the command to add a secret is:

aws ssm put-parameter \
    --name "/example/hierarchy/secret" \
    --type "String" \
    --value "ami-12345abcdeEXAMPLE"

You can also list all parameters stored directly in a path with:

aws ssm get-parameters-by-path --path /example/hierarchy

These secret paths should match the paths found in the template.yaml file as values for certain environment variables. An example is

NICE_HASH_ORG_ID: '{{resolve:ssm:/nice_hash/organization_id:1}}'

In this case, you would want to run the following command to store the secret with ssm:

aws ssm put-parameter \
    --name "/nice_hash/organization_id" \
    --type "String" \
    --value "1234"

Deploy the application

Note that this project already comes with a samconfig.toml file for ease of use. With that file, you can build and deploy with the following:

sam build --use-container
sam deploy

The first sam command will build the source of the application. The second sam command will package and deploy the application to AWS.

To build and deploy with a walkthrough to customize changes to the config, run the following in your shell:

sam build --use-container
sam deploy --guided

The second sam command will trigger a series of prompts to customize the deployment configuration:

  • Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching the project name.
  • AWS Region: The AWS region you want to deploy the app to.
  • Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
  • Allow SAM CLI IAM role creation: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the CAPABILITY_IAM value for capabilities must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass --capabilities CAPABILITY_IAM to the sam deploy command.
  • Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run sam deploy without parameters to deploy changes to the application.

Use the SAM CLI to build and test locally

Build the application with the sam build --use-container command.

sam build --use-container

For rebuilding small changes, it should be a lot faster to use the following command:

sam build --use-container --parallel --skip-pull-image

The SAM CLI installs dependencies defined in requirements.txt files for each function, creates a deployment package, and saves it in the .aws-sam/build folder.

Before invoking a function, make sure you have any necessary secrets in an env.json file as that will be necessary to override any environment variables referencing AWS Systems Manager strings. The SAM CLI does not currently support dynamic resolution of those references running locally. An example_env.json is provided for you to show what it could look like for this project.

Test a single function by invoking it directly.

Run functions locally and invoke them with the sam local invoke command. Note the use of --env-vars to pull values from the file mentioned above.

sam local invoke --env-vars env.json ExampleFunction

Start an API locally.

sam local start-api

Fetch, tail, and filter Lambda function logs

To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by the deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.

NOTE: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.

sam logs -n ExampleFunction --stack-name myb-nicehash-withdrawal --tail

You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.

Testing

Note that currently only local testing is supported.

Before running tests for the lambda functions, we need to host them locally. This can be done with the following command which starts by ensuring the code is built and then starts the lambda environment:

sam build --use-container --parallel --skip-pull-image
sam local start-lambda --env-vars env.json

Once that has been done, you can run the tests using:

pytest --verbose

Or a specific test using:

pytest --verbose tests/test_specific_function.py

Cleanup

To delete this application after it has been deployed successfully, use the AWS CLI. Assuming you used the project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name myb-nicehash-withdrawal

Resources

See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.

Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: AWS Serverless Application Repository main page